Help with my C++ Program

This is a discussion on Help with my C++ Program within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a few problems with my program. First off, let me tell you what the program is. The ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    16

    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;
     char answer;
     
     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 << "\n     *MENU*\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';
      cin >> answer;
      
      switch(answer)
     {
      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;
     }
      
     } while (answer != '0');
     system("pause");
      return 0;
    }
      
    double sales_tax(double running_total)
    {
      return running_total * 1.07;
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    while (answer != 'T' || answer != 't');

    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;

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    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'

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> while (answer != 'T' || answer != 't');
    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. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    That tells them if they enter an invalid choice. They can only enter S, C, P, B, R, L, X, or T.

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    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. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    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. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    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. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    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;
    Last edited by JDK721; 12-03-2006 at 09:46 PM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Alrighty.. I got it, thanks for the help!
    Last edited by JDK721; 12-03-2006 at 09:55 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21