# Thread: Help with my C++ Program

1. ## Help with my C++ Program

Hello, I have a few problems with my program. First off, let me tell you what the program is.

The snack bar sells only six different items : a sandwich, chips, pickle, brownie, regular drink, and a large drink. All items are subject to sales tax. Set prices for the products.

The program should repeatedly display the menu below until the sale is totaled. The program should keep a running total of the amount of the sale based on the costs that you place in constants for each of the food items. The running total should be displayed somewhere each the menu is displayed again.

S - Sandwich
C - Chips
P - Pickle
B - Brownie
R - Regular Drink
L - Large Drink
X - Cancel and start over
T - Total the sale

If the sale is canceled, clear your running total and display the menu again. When the sale is totaled, calculate the sales tax based on 7%. Print the final total due on the screen.

You can use your own functions to design a solution to the problem. You are required to use a function to calculate the sales tax. Other use of functions is up to you.

Here is what I have so far, my problems are.. I need help with the while statement, I have no clue what to put for it in order for it to work properly. Second, it is saying \$4 instead of \$4.00 for sandwich, etc. even though I assigned sandwich to 4.00

Code:
```#include <iostream.h>
#include <string.h>
#include <iomanip.h>

double sales_tax(double running_total);

main()
{
double sandwich = 4.00;
double chips = 1.75;
double pickle = 0.50;
double brownie = 1.00;
double regular_drink = 1.25;
double large_drink = 2.00;
double salestax;
double total_sale, running_total = 0;

do
{
cout << "Enter what you would like, enter S to order a sandwich, C to order chips, etc.\n";
cout << "Enter X to cancel your sale and start over, and when you're finished ordering enter T to total your sale.\n";

cout << "S - Sandwich: \$" << sandwich << '\n';
cout << "C - Chips: \$" << chips << '\n';
cout << "P - Pickle: \$" << pickle << '\n';
cout << "B - Brownie: \$" << brownie << '\n';
cout << "R - Regular drink: \$" << regular_drink << '\n';
cout << "L - Large drink: \$" << large_drink << '\n';
cout << "X - Cancel sale and start over\n";
cout << "T - Total the sale\n\n";

cout << "Your total so far is \$" << running_total << '\n' << '\n';

{
case 'S':
running_total = running_total + sandwich;
break;
case 'C':
running_total = running_total + chips;
break;
case 'P':
running_total = running_total + pickle;
break;
case 'B':
running_total = running_total + brownie;
break;
case 'R':
running_total = running_total + regular_drink;
break;
case 'L':
running_total = running_total + large_drink;
break;
case 'X':
running_total = 0;
break;
case 'T':
running_total = sales_tax(running_total);
break;
default:
answer != 'S', 'C', 'P', 'B', 'R', 'L', 'X', 'T' << '\n';
cout << "\nPlease enter an acceptable choice.\n\n";
break;
}

system("pause");
return 0;
}

double sales_tax(double running_total)
{
return running_total * 1.07;
}```

for your second question, you can either display a string

cout<<"S - Sandwich: \$4.00"

or

Code:
```if ((int)(sandwich*100)%100 == 0)
cout<<(int)sandwich<<".00";
else
cout<<(int)sandwich<<"."<<sandwich*100-(int)sandwich*100;```

3. Okay thanks, the while loop works now.. but it isn't calculating the tax right on some situations! Oh yeah and for the second question, it displays the menu prices right.. but when it gives me the running total, etc. it says \$4 instead of \$4.00

I order a sandwich and chips, which brings me to \$5.75 and total it.. it says '\$6.1525'

This will always evaluate to true. You probably meant &&.

>> it says \$4 instead of \$4.00
Use the fixed and precision manipulators from <iomanip>.

>> answer != 'S', 'C', 'P', 'B', 'R', 'L', 'X', 'T' << '\n';
What are you trying to do there?

5. That tells them if they enter an invalid choice. They can only enter S, C, P, B, R, L, X, or T.

6. Just remove the
Code:
`   answer != 'S', 'C', 'P', 'B', 'R', 'L', 'X', 'T' << '\n';`
The code inside default: will be executed if the switch's value is not in the other cases.

7. Did you mean to output that or was that meant as code. I'm surprised it compiles. Either put it in quotes and send it to cout if you meant to output it, or explain what that specific line of code is supposed to do and maybe someone can help you change it to something meaningful. Or you could just remove it.

8. My last problem is.. when they hit T I need it to add the current total to the amount of sales tax and for it to say "Your final total is ___"

Its just saying "Your running total is __"

9. It sounds like it is continuing the loop again. Are you using h_howee's suggestion without fixing it?

If you want to output something with the total, put code in under the 'T' case to do that.

10. I don't understand how to make it when they press T to say "Your total sale is ___" and add the running total to the sales tax instead of continuing to say Your total so far is.

11. Right. Fix your while loop so that it ends when 'T' is entered. h_howee's suggestion was close but not quite right.

Then, add code under the 'T' case to output what you want to output.

12. I put this.. it still is saying your total so far is, and what variable do I input for it?

Code:
```case 'T':
running_total = sales_tax(running_total);
cout << "Your total sale including tax is \$" << running_total (what variable do I put here) << '\n';
break;```

13. >> running_total (what variable do I put here)
running_total is a variable that you just assigned the total plus tax to. You don't need anything else, that looks good.

>> it still is saying your total so far is
What does the while condition of your loop look like? You want it to loop while the answer does not equal 'T'.

14. Alrighty.. I got it, thanks for the help!

15. I said you want it to loop while the answer does not equal 'T'. That is the opposite of looping until answer does not equal 'T'.

A loop condition specifies what should be true for the loop to keep going. I just changed my advice to be an even bigger hint because it matches how the code looks.