Simple addition program

Alright guys, I was hoping someone could help me with this. Simple vending machine program. Only thing I'm missing is the grandTotal. I want to be able to tally up the price of the grand total at the very end of the program. For example, if I select 'A' for coffee and select 3 cups it'll cost $3 but then the program will ask if I want another beverage and if I select 'B' afterwards and select 2 cups for 'B' for example I want it to be able to add the total for the first selection to the second selection as well before I select 'E' to end the program and print out the grandTotal. So technically I would have $3 for the coffee/A and $1.50 for the tea/B, which would be $4.5 total. Where would I insert the code for that operation? I'm completely lost.

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

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    char choice;
    double cups;
    double amount;
    double grandTotal;

    do                                                      
    {
        cout << "Hot Beverage Menu" << endl;
        cout << "-----------------" << endl;
        cout << "A: Coffee $1.00" << endl;
        cout << "B: Tea $0.75" << endl;
        cout << "C: Hot Chocolate $1.25" << endl;
        cout << "D: Cappuccino $2.50" << endl;
        cout << "E: exit/end" << endl;
        cout << "Please make a selection: " << endl;

        cin >> choice;

        switch (choice)
        {

        case 'A': cout << "Coffee $1.00 " << endl;          
        cout << "How many cups?: ";
        cin >> cups;
        amount = 1.00 * cups;
        cout << "Total is $" << amount;
        break;

        case 'B': cout << "$Tea $0.75 " << endl;            
        cout << "How many cups?: ";                          
        cin >> cups;
        amount = 0.75 * cups;
        cout << "Total is $" << amount;
        break;

        case 'C': cout << "Hot Chocolate $1.25 " << endl;   
        cout << "How many cups?: ";                         
        cin >> cups;
        amount = 1.25 * cups;
        cout << "Total is $" << amount;
        break;

        case 'D': cout << "Cappuccino $2.50 " << endl;      
        cout << "How many cups?: ";                         
        cin >> cups;
        amount = 2.50 * cups;
        cout << "Total is $" << amount;
        break;
        }

    } while (choice == 'A' || choice == 'B' || choice == 'C' || choice == 'D');     //end of loop

        cout << "Grand total is $ " << grandTotal;
    return 0;
}

The easiest way to do this is to use your amount variable to keep track of the total. You can use the += operator to add to the amount every time another selection is made, for example:
amount += 2.50 * cups;

Then, all you have to do is print out amount at the end of your program.
Thanks yulingo
Topic archived. No new replies allowed.