Switch Statement; Summing items

I'm creating a program that is combining functions to show a menu to a user then the user picks numbered items which results in the totalling of a bill for them.

Where I'm stuck is after they're entered their selections, how to take them and sum them up. I've been trying with a switch statement, but I'm not sure if this is the right method. Is it even possible to sum separate cases of a switch statement together? Below is the code I've gotten so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
using namespace std;

void showMenu ();
void showBill (double, double, float, double);
void changeDue (double, int);

int main()
{
    int choice, num;
    double amount = 0;
    const int CHOICE_HAMBURGER = 1, CHOICE_HOTDOG = 2, CHOICE_PEANUTS = 3, CHOICE_POPCORN = 4, CHOICE_SODA = 5, CHOICE_CHIPS = 6, CHOICE_WATER = 7, CHOICE_QUIT = 8;
    const double HAMBURGER = 6.00, HOTDOG = 4.50, PEANUTS = 3.75, POPCORN = 5.50, SODA = 2.80, CHIPS = 1.00, WATER = 2.00;
    
    do
    {
        showMenu();
        
        while (choice != CHOICE_QUIT)
        {
            cout << "Enter menu item: ";
            cin >> choice;
            
          
            switch (choice)
            {
                case 1: amount = HAMBURGER;
                case 2: amount = HOTDOG;
                case 3: amount = PEANUTS;
                case 4: amount = POPCORN;
                case 5: amount = SODA;
                case 6: amount = CHIPS;
                case 7: amount = WATER;
            }
            if (choice < CHOICE_HAMBURGER || choice > CHOICE_QUIT)
            {
                cout << "Please enter a valid menu choice: ";
                cin >> choice;
            }
            
        }

        
    } while (choice != CHOICE_QUIT);
    
    return 0;
}

//DISPLAY THE MENU FUNCTION--------------------------------------
void showMenu ()
{
    cout  << "\n\t\tBaseball Game Snacks\n\n"
          << "1. Hamburger $6.00\n"
          << "2. Hotdog    $4.50\n"
          << "3. Peanuts   $3.75\n"
          << "4. Popcorn   $5.50\n"
          << "5. Soda      $2.80\n"
          << "6. Chips     $1.00\n"
          << "7. Water     $2.00\n"
          << "8. End Order\n\n";
    
}

//DISPLAY THE BILL FUNCTION--------------------------------------
void showBill (double bill, double totBill, float tax, double tip)
{
 //haven't created yet
    
}

//DISPLAY THE CHANGE DUE FUNCTION--------------------------------
void changeDue (double totBill, int amtTendered)
{
 //haven't created yet
    
You may consider enumerating the menu, Arrays start with a zero index so use 0 for CHOICE_HAMBURGER:

 
enum CHOICE {CHOICE_HAMBURGER = 0, CHOICE_HOTDOG, CHOICE_PEANUTS, CHOICE_POPCORN, CHOICE_SODA, CHOICE_CHIPS, CHOICE_WATER, CHOICE_QUIT};


Store the prices in an array:
 
const double price[] = {HAMBURGER, HOTDOG, PEANUTS, POPCORN, SODA, CHIPS, WATER};


Select the price from the array via the enumerated index and sum those DURING the selection process, on the fly so to speak. The switch is entirely obsolete in my opinion

sum = sum + price[choice];

EDIT: You have to check if the user enters a value that is within the CHOICE_HAMBURGER to CHOICE_WATER range otherwise you'll risk runtime errors.




Last edited on
worked like a charm! thanks!

here's what the final program looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <iomanip>
using namespace std;

void showMenu ();
void showBill (float, float, float, float);
void changeDue (float, float);

int main()
{
    float taxCalc, tipCalc, amountDue, tendered;
    int choice;
    float sum = 0;
    enum CHOICE {CHOICE_HAMBURGER = 0, CHOICE_HOTDOG=1, CHOICE_PEANUTS=2, CHOICE_POPCORN=3, CHOICE_SODA=4, CHOICE_CHIPS=5, CHOICE_WATER=6, CHOICE_QUIT=7};
    const float HAMBURGER=6.00, HOTDOG=4.50, PEANUTS=3.75, POPCORN=5.50, SODA=2.80, CHIPS=1.00, WATER=2.00;
    const float price[] = {HAMBURGER, HOTDOG, PEANUTS, POPCORN, SODA, CHIPS, WATER};
    
    do
    {
        showMenu();
        
        while (choice != CHOICE_QUIT)
        {
            cout << "Enter menu item: ";
            cin >> choice;
            
            if (choice < CHOICE_HAMBURGER || choice > CHOICE_QUIT)
            {
                cout << "Please enter a valid menu choice: ";
                cin >> choice;
            }
            
            sum = sum + price[choice];
            cout<<setprecision(2)<<fixed;
            
            taxCalc = .065 * sum;
            tipCalc = .20 * sum;
            amountDue = taxCalc + tipCalc + sum;
        }
        showBill(sum, amountDue, taxCalc, tipCalc);
        
        cout<<"Enter amount tendered: ";
        cin>>tendered;
        
        changeDue(amountDue, tendered);
        
    } while (choice != CHOICE_QUIT);
    
    return 0;
}

//DISPLAY THE MENU FUNCTION--------------------------------------
void showMenu ()
{
    cout  << "\n\t\tBaseball Game Snacks\n\n"
          << "0. Hamburger $6.00\n"
          << "1. Hotdog    $4.50\n"
          << "2. Peanuts   $3.75\n"
          << "3. Popcorn   $5.50\n"
          << "4. Soda      $2.80\n"
          << "5. Chips     $1.00\n"
          << "6. Water     $2.00\n"
          << "7. End Order\n\n";
}

//DISPLAY THE BILL FUNCTION--------------------------------------
void showBill (float bill, float totBill, float tax, float tip)
{
    cout<<"\nBill = $"<<bill<<endl;
    cout<<"Tax = $"<<tax<<endl;
    cout<<"Tip = $"<<tip<<endl;
    cout<<"Total amount due = $"<<totBill<<endl;
}

//DISPLAY THE CHANGE DUE FUNCTION--------------------------------
void changeDue (float totBill, float amtTendered)
{
    float change = amtTendered - totBill;
    cout<<"Change due = $"<<change;
}
you may consider enumerating the menu


or you could use a map :)

1
2
3
4
5
6
	std::map <std::string, double> foodMap;

	foodMap["Hamburger"] = 6.0;
	foodMap["Hotdog"] = 4.5;
	foodMap["Peanuts"] = 3.75;
	foodMap["Popcorn"] = 5.5;
Last edited on
Topic archived. No new replies allowed.