Not sure how to add a running total

I need to add a running total to this, that adds the users price in case they want more pizza, but im not sure where to put it. (fairly new to this)

Here's the code:




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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;


void displayPizzaMenu();

void displaySizeMenu();

int getPizzaChoice();

int getSizeChoice();

void totalprice (int, double);



int main()
{


const double personal = 10.00,  /// aka num 2  
             medium = 14.50,
            large = 19.00,
            extralarge = 23.50;

const int MEAT_CHOICE = 1,
        VEGGIE_CHOICE =2,
        HAWAII_CHOICE =3,
        PHILLY_CHOICE =4,
        BBQ_CHOICE = 5,
        EXIT_CHOICE = 6;

const int PERSONAL_SIZE = 1,
            MEDIUM_SIZE =2,
            LARGE_SIZE =3,
            EXTRA_LARGE =4;

int pizzaChoice; // holds menu choice
int sizechoice; // holds size 
int howMany; //how many they want aka   num 1
cout<< setprecision(2)<<fixed<<showpoint;


cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<< " Welcome to Ian's Pizza of West Chester! \n";
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<endl;




do{

displayPizzaMenu ();
getPizzaChoice ();

cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

displaySizeMenu();
getSizeChoice();

sizechoice= getSizeChoice();
switch (sizechoice)
{
case 1:
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<" How many personal pizzas?  ";
cin>>howMany;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<endl;
cout<< fixed << showpoint << setprecision(2);
cout<< "Your order total is  $";
totalprice(howMany, personal);
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
break;


case 2:
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<" How many medium pizzas?  ";
cin>> howMany;
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<< "Your total price is  $";
cout<< fixed << showpoint << setprecision (2);
totalprice(howMany, medium);
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
break;


case 3:
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<" How many large pizzas?  ";
cin>>howMany;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<< " Your order total is  $";
cout<< fixed << showpoint << setprecision (2);
totalprice(howMany, large);
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
break;

case 4:
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<" How many extra large pizzas?  ";
cin>>howMany;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<< "Your order total is $";
cout<< fixed << showpoint << setprecision (2);
totalprice(howMany, extralarge);
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
break;

    }

} while (sizechoice <= 4);






return 0;

}





void displayPizzaMenu()
{
cout<<"Specialty Pizza Menu \n"
<< "1) Meat Lovers \n"
<< "2) Vegetartian \n"
<< "3) Hawaiian \n"
<< "4) Philly Steak \n"
<< "5) BBQ Chicken \n"
<< "6) Exit Menu \n";
}




void displaySizeMenu()
{
cout<<"Available Sizes and Prices \n"
<<endl
<< "1) 10 Personal       - $10.00 \n"
<< "2) 14 Medium         - $14.50\n"
<< "3) 16 Large          - $19.00\n"
<< "4) 18 Extra Large    - $23.50\n";

}
 



int getPizzaChoice()
{
  int pizzaChoice;
  cout<< "Your choice (1-6)?  ";
  cin>>pizzaChoice;

while (pizzaChoice < 1 || pizzaChoice > 6)
{
cout<< pizzaChoice << " is not a valid choice. Please enter 1-6. \n"; 
cout<< "your choice?   ";
cin>>pizzaChoice;
}
 if ( pizzaChoice == 6)
  {
cout<< " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<<" Thank you for visiting Ian's Pizza of West Chester!  Come back Soon!\n"
<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<endl;
  exit(-1);
  }
   pizzaChoice++;
  return pizzaChoice;
}



int getSizeChoice()
{
 int sizechoice;
cout<<" Your Choice (1-4)?   ";
cin>>sizechoice;

if ((sizechoice > 4 ) || (sizechoice < 0))
{
cout<< " please enter a valid choice (1-4)\n";
cin>>sizechoice;
}
return sizechoice;
}

void totalprice (int num1, double num2)
{
cout<<(num1 * num2)<<endl;

}
Last edited on
You will receive faster, better help if you format your source code to make it easier to read.
How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.
How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post and add code tags.


You call getSizeChoice(); but never use its value. sizechoice in main is uninitialized: undefined behavior.
Last edited on
You must use "code tags" when posting code, like this:

[code]
Always put your code between code tags!
[/code]

As for your problem, sizechoice will not magically receive the value from getSizeChoice().
You need to say:

 
sizechoice = getSizeChoice();

Okay I'll make sure to do that, thank you for you help.
Line 64 can be deleted
thanks!
FWIW: This doesn't solve your immediate problem but shows how you can take advantage of re-organizing the data, simplifying and especially removing duplication.

Each one of the commented sections can be put into functions, leaving a while loop with appropriate exits in main.

All the fiddly stuff about presentation is a low priority.

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
#include <iostream>
#include <string>

int main()
{
    std::string pizza_type[]
    {"Meat Lovers","Vegetarian","Hawaiian","Philly Steak","BBQ Chicken"};
    
    std::string pizza_size[]
    {"Personal","Medium","Large","Extra Large"};
    
    double price_list[]{10.00,14.50,19.00,23.50};
    
    // SELECT TYPE
    for(int i = 0; i < 5; i++)
        std::cout << i << ' ' << pizza_type[i] << '\n';
    
    int type_no{0};
    std::cout << "Enter type: ";
    std::cin >> type_no;
    
    // SELECT SIZE
    for(int i = 0; i < 4; i++)
        std::cout << i << ' ' << pizza_size[i] << " $" << price_list[i] << '\n';
    
    int size_no{0};
    std::cout << "Enter type: ";
    std::cin >> size_no;
    
    // ENTER HOW MANY
    int how_many{0};
    std::cout << "Enter how many: ";
    std::cin >> how_many;
    
    // CALCULATE PRICE
    double price = price_list[size_no] * how_many;
    std::cout
    << how_many << ' ' << pizza_type[type_no] << " - "
    << pizza_size[size_no]
    << " @ $" << price_list[size_no] << " = $" << price << '\n';
    
    // CALCULATE GRAND TOTAL
    double grand_total{0};
    grand_total += price;
    std::cout << "TOTAL $" << grand_total << '\n';
    
    return 0;
}

0 Meat Lovers
1 Vegetarian
2 Hawaiian
3 Philly Steak
4 BBQ Chicken
Enter type: 1
0 Personal $10
1 Medium $14.5
2 Large $19
3 Extra Large $23.5
Enter type: 2
Enter how many: 3
3 Vegetarian - Large @ $19 = $57
TOTAL $57
Program ended with exit code: 0
One of the difficulties with the lack of a full problem description is whether individual purchases are to be saved, which is one way to produce an itemised list, or whether you just want a running total as orders are made. All this is possible but depends on how far down the learning track you are.

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
#include <iostream>
#include <string>

int main()
{
    std::string pizza_type[]
    {"Meat Lovers","Vegetarian","Hawaiian","Philly Steak","BBQ Chicken"};
    
    std::string pizza_size[]
    {"Personal","Medium","Large","Extra Large"};
    
    double price_list[]{10.00,14.50,19.00,23.50};
    
    double grand_total{0};
    char ch{'*'};
    
    while(std::cout << "c to continue q to quit " and std::cin >> ch and ch != 'q')
    {
        // SELECT TYPE
        for(int i = 0; i < 5; i++)
            std::cout << i << ' ' << pizza_type[i] << '\n';
        
        int type_no{0};
        std::cout << "Enter type: ";
        std::cin >> type_no;
        
        // SELECT SIZE
        for(int i = 0; i < 4; i++)
            std::cout << i << ' ' << pizza_size[i] << " $" << price_list[i] << '\n';
        
        int size_no{0};
        std::cout << "Enter type: ";
        std::cin >> size_no;
        
        // ENTER HOW MANY
        int how_many{0};
        std::cout << "Enter how many: ";
        std::cin >> how_many;
        
        // CALCULATE PRICE
        double price = price_list[size_no] * how_many;
        std::cout
        << how_many << ' ' << pizza_type[type_no] << " - "
        << pizza_size[size_no]
        << " @ $" << price_list[size_no] << " = $" << price << '\n';
        
        // CALCULATE GRAND TOTAL
        grand_total += price;
    }
    
    std::cout << "TOTAL $" << grand_total << '\n';
    
    return 0;
}


c to continue q to quit c
0 Meat Lovers
1 Vegetarian
2 Hawaiian
3 Philly Steak
4 BBQ Chicken
Enter type: 3
0 Personal $10
1 Medium $14.5
2 Large $19
3 Extra Large $23.5
Enter type: 2
Enter how many: 2
2 Philly Steak - Large @ $19 = $38
c to continue q to quit c
0 Meat Lovers
1 Vegetarian
2 Hawaiian
3 Philly Steak
4 BBQ Chicken
Enter type: 2
0 Personal $10
1 Medium $14.5
2 Large $19
3 Extra Large $23.5
Enter type: 3
Enter how many: 3
3 Hawaiian - Extra Large @ $23.5 = $70.5
c to continue q to quit q
TOTAL $108.5
Program ended with exit code: 0
Thank you guys seriously ^^^
Topic archived. No new replies allowed.