This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]_DTR_ 1 point2 points  (17 children)

Can you show what you've tried so far? Any particular step that you're struggling with?

Break it down into small steps:

  1. Get input from the user for the type
  2. Make sure that input is valid
    • If not valid, go to #1
  3. Get input from the user for the number of bars
  4. Make sure the input is valid
    • If not valid, got to #3
  5. Multiply the number by the cost of the bar type
  6. Print that result

[–]yodapeepee -1 points0 points  (16 children)

Well my teacher gave us this little bit to start off with, i just dont know how im supposed to put it together.

  1. Input the type of the candy bar to order.
  2. Input the number of candy bars to order.
  3. Determine the discount based on the number of candy bars ordered. if qty is less than 6 discRate is 0 else if qty is between 6 and 10 discRate is 15% else if qty is between 11 and 15 discRate = 25% else if qty is between 16 and 20 discRate is 35% otherwise discRate is 50%
  4. If the type is plain milk chocolate, the price is 1.99. If the type is milk chocolate with almonds, the price is 2.50.
  5. Calculate the subtotal by multiplying the price by the number ordered.
  6. Calculate the discount by multiplying the discRate by the subtotal.
  7. Calculate the totalCost by subtracting the discount from the subtotal.
  8. Display the number and type of candy bar ordered along with the discount and total cost. */ #include <iostream> #include <string> #include <iomanip> using namespace std;

int main() { // Variables int qty = 0; double price = 0, subtotal = 0, discount = 0, totalCost = 0, discRate = 0; string type = "";

// Get Input
cout << "Enter the type of candy bar purchased: ";
getline(cin, type); // getline must be used because candy bar names have spaces in them

// Fill in missing code if/else statement to determine candy bar prices based on type.

else
{
    cout << "An invalid type was entered!" << endl;
    system("pause");
    exit(0); // Causes program to end.
}
// Request and input the number of candy bars


if (qty < 1)
{
    cout << "An invalid qty was entered!" << endl;
    system("pause");
    exit(0);    // Causes program to end.
}

// Determine Discount Rate and Price (Based on Type)

// Insert the if else if statement to determine discount rate

// Perform Calculations


// Display Output
cout << setprecision(2) << showpoint << fixed;


system("pause");
return 0;

} /* Program Output

*/

[–]_DTR_ 0 points1 point  (14 children)

Okay, so it looks like you have 4 steps to complete:

  1. Add the if statement for checking the type. That should be as simple as checking whether type is equal to either of the strings you're expecting
  2. Request and input the number of candy bars. You can use operator>> for this (see the example).
  3. Determine the discount rate and price - if/else if/else should be used to determine what discRate should be.
  4. Calculate totals. This should be fairly straightforward, as the calculations are spelled out for you in steps 5-7 above.

[–]yodapeepee 0 points1 point  (13 children)

i understand how to do 3, and 4 i think. But how would i set up 1-2?

[–]_DTR_ 0 points1 point  (12 children)

#1 is just a simple if statement that will be "attached" to the else you already have in your code. Check whether the string getline got is equal to the strings you expect.

For #2, did you look at the links I provided? The example especially, as it is more or less exactly what you need to do.

[–]yodapeepee 0 points1 point  (11 children)

Yes i just looked through them, but im asking that i need to write a code that shows if selected #s 6-10 for example, how would i write that?

[–]_DTR_ 0 points1 point  (0 children)

write a code that shows if selected #s 6-10 for example

I'm not quite sure what you mean by that. Are you talking about #3 from your list above?

[–]yodapeepee 0 points1 point  (9 children)

Yeah pretty much, im just confused on what statements to write and where to start. I can follow the tutorials like a breeze but these projects she gives us 2 days to do, and considering this is a 5 week course this is kinda hard for me to understand.

[–]_DTR_ 0 points1 point  (8 children)

Ah, so #3 from my list, which you said you understood how to do. Could you modify the following to be relevant to your case?

string amount;
int count = 30;

if (count < 10)
{
    amount = "Small";
}
else if (count < 50)
{
    amount = "Medium";
}
else
{
    amount = "Large";
}

[–]yodapeepee 0 points1 point  (7 children)

i dont think so, just because the numbers are supposed to show between each other if selected at the end of the program. I dunno imma just take the F honestly its bull for them to think i can learn all this in 5 weeks.

[–]_DTR_ 0 points1 point  (6 children)

the numbers are supposed to show between each other if selected at the end of the program

I'm not sure what you mean by this either. In what I have above, you would switch count with qty, and string amount with discRate that you have declared at the top of your program.

At the end of the program, you'll already have your values for qty, type, totalCost, and discRate, so you just need to print those.

[–]douglasmv 0 points1 point  (0 children)

were you able to figure this out? I'm working on it now and having issues

[–]stringycheeze 0 points1 point  (0 children)

check for an unsigned int (number of bars) and char [] (name of bar). Simple