Having issues making a menu that will subtotal and then display a grand total. My professor is no help. It would be awesome if anyone can point me in the right direction. Because i have no idea why its not working.
The program works fine until I get to the grand total, then it will not add together all the orders placed. Please help.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
   // Constants for menu choices
   const int COFFEE_CHOICE = 'A',
             TEA_CHOICE = 'B',
             HOT_CHOCOLATE_CHOICE = 'C',
             CAPPUCCINO_CHOICE = 'D',
             EXIT_CHOICE = 'E';

   // Constants drink prices
   const double COFFEE = 1.00,
                TEA = 0.75,
                HOT_CHOCOLATE = 1.25,
                CAPPUCCINO= 2.50;

   // Variables
   char choice;       // Menu choice
   int number;       // Number of cups
   double charges;   // Monthly charges
   double TOTAL = 0.0;

   // Output formatting.
   cout << fixed << showpoint << setprecision(2);

      // Display the drink menu.
      cout << "\n\t\tHot Beverage Menu\n\n"
           << "A. Coffee           $1.00\n"
           << "B. Tea              $ .75\n"
           << "C. Hot Chocolate    $1.25\n"
           << "D. Cappuccino       $2.50\n"
           << "E. Exit\n\n"
           << "Please enter your choice: ";
      cin >> choice;

      // Validate the menu selection.
      while (choice < COFFEE_CHOICE || choice > EXIT_CHOICE)
         cout << "Please enter a valid selection: ";
         cin >> choice;

      // Validate and process the user's choice.
      if (choice != EXIT_CHOICE)
         // Get the number of cups.
         cout << "How many cups would you like? ";
         cin >> number;

         // Respond to menu selection.
         switch (choice)
            case COFFEE_CHOICE:
                charges = number * COFFEE;
            case TEA_CHOICE:
                charges = number * TEA;
            case HOT_CHOCOLATE_CHOICE:
                charges = number * HOT_CHOCOLATE;
            case CAPPUCCINO_CHOICE:
                charges = number * CAPPUCCINO;
         // Display the total charges.
         cout << "Your sub total is $" << charges << endl;
         else if (choice == EXIT_CHOICE)
             TOTAL += charges;
             cout << "Your grand total is $" << TOTAL << endl;
   while (choice != EXIT_CHOICE);
   return 0;

Recommended Answers

All 2 Replies

The issue is that you set charges=number*PRICE_CONSTANT each time, without saving the old value. Basically here is what happens if you trace charges (program tracing is a very valuable debugging tool):

Charges: ??? (since its un-initiated it could be anything, changing line 24 to double charges=0.0; would fix this)
Choice: Coffee, number: 2
Charges: 2.00 (charges=number*COFFEE;//=2*1.00=2.00)
Choice: Hot Chocolate, number: 1
Charges: 1.25 (note that the old value of 2.00 is gone)
Choice: EXIT
Charges: 1.25, TOTAL: 1.25

The solution is thus to add charges to total as you create them. Moving line 77 to just after line 71 should fix your problem.

Your amazing! Is it too soon to tell you I love you? lol. Ive been beating my head against the wall all morning. Why is it its always a simple solution? Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.