How to total the price from looping?

Hey guys how can I total the price from doing multiple loops? The total price is taken from the very last loop. I want to total the purchases together into one big total amount to pay for the user. Maybe I made my code harder on myself than it should be. Please help me with any examples and advice.

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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  #include <iostream>
#include <string>
using namespace std;

int main ()
{

int code, size, quantity;
double price, total;
string response;


cout << "Tom's Fish Stock" << endl;
cout << "Price per fish" << endl;
cout << "__________________________________________________________________________________________________" << endl;
cout << "Item code     Species           Sizes (Price per fish)             *Except as noted*" << endl;
cout << "__________________________________________________________________________________________________" << endl;
cout << "           Gamefish             Size 1          Size 2          Size 3          Size 4" << endl;
cout << "__________________________________________________________________________________________________" << endl;
cout << "1111       Bluegill             1-3 in.($0.79)  3-5 in.($0.99)  5-7 in.($1.39)" << endl;
cout << "1112       Redear Sunfish       1-3 in.($0.85)  3-5 in.($1.09)  5-7 in.($1.69)" << endl;
cout << "1113       Green Sunfish        1-3 in.($0.85)  3-5 in.($1.19)  5-6 in.($1.79)" << endl;
cout << "1114       Bluegill Hybrid      1-3 in.($0.85)  3-5 in.($1.09)  5-7 in.($1.49)" << endl;
cout << "1115       Yellow Perch         1-3 in.($0.99)  3-6 in.($1.25)  6-9 in.($1.99)" << endl;
cout << "1116       Channel Catfish      1-3 in.($0.79)  3-6 in.($1.10)  7-10 in.($1.59)  10-13 in. ($2.30)" << endl;
cout << "1117       Largemouth Bass      1-3 in.($0.89)  4-7 in.($1.19)  7-10 in.($1.69)  10-13 in. ($2.59)" << endl;
cout << "1118       Smallmouth Bass      1-3 in.($0.99)  4-7 in.($1.59)  7-10 in.($1.99)  10-13 in. ($2.99)" << endl;
cout << "__________________________________________________________________________________________________" << endl;
cout << "           Feeder Fish" << endl;
cout << "__________________________________________________________________________________________________" << endl;
cout << "1119       Golden Shiner *(per 50)*             3-4 in.($7.99)" << endl;
cout << "1120       Fathead Minnow *(per 100)*           2-3 in.($5.99)" << endl;
cout << "__________________________________________________________________________________________________" << endl;
cout << "           Decorative Pond Fish" << endl;
cout << "__________________________________________________________________________________________________" << endl;
cout << "1121       Koi Goldfish                         2-4 in.($1.29)" << endl;


cout << "\n" << "Welcome to Tom's Fish Stock!" << endl;

cout << "Please enter the Item code to purchase a species of fish." << endl;
cin >> code;

if (code == 1111) 
	cout << "You have selected Bluegill." << endl;
else if (code == 1112)
	cout << "You have selected Redear Sunfish." << endl;
else if (code == 1113)
	cout << "You have selected Green Sunfish." << endl;
else if (code == 1114)
	cout << "You have selected Bluegill Hybrid." << endl;
else if (code == 1115) 
	cout << "You have selected Yellow Perch." << endl;
else if (code == 1116) 
	cout << "You have selected Channel Catfish." << endl;
else if (code == 1117)
	cout << "You have selected Largemouth Bass." << endl;
else if (code == 1118)
	cout << "You have selected Smallmouth Bass." << endl;
else if (code == 1119)
	cout << "You have selected Golden Shiner." << endl;
else if (code == 1120)
	cout << "You have selected Fathead Minnow." << endl;
else if (code == 1121)
	cout << "You have selected Koi Goldfish." << endl;

cout << "Please enter a size number (1-4)" << endl;
cin >> size;

if (code == 1111) 
{
	if (size == 1)
	{price = 0.79;}

	else if (size == 2)
	{price = 0.99;}

	else if (size == 3)
	{price = 1.39;}
}

if (code == 1112) 
{
	if (size == 1)
	{price = 0.85;}

	else if (size == 2)
	{price = 1.09;}

	else if (size == 3)
	{price = 1.69;}
}

if (code == 1113) 
{
	if (size == 1)
	{price = 0.85;}

	else if (size == 2)
	{price = 1.19;}

	else if (size == 3)
	{price = 1.79;}
}

if (code == 1114)
{
	if (size == 1)
	{price = 0.85;}

	else if (size == 2)
	{price = 1.09;}

	else if (size == 3)
	{price = 1.49;}
}

if (code == 1115)
{
	if (size == 1)
	{price = 0.99;}

	else if (size == 2)
	{price = 1.25;}

	else if (size == 3)
	{price = 1.99;}
}

if (code == 1116)
{
	if (size == 1)
	{price = 0.79;}

	else if (size == 2)
	{price = 1.10;}

	else if (size == 3)
	{price = 1.59;}

	else if (size == 4)
	{price = 2.30;}
}

if (code == 1117)
{
	if (size == 1)
	{price = 0.89;}

	else if (size == 2)
	{price = 1.19;}

	else if (size == 3)
	{price = 1.69;}

	else if (size == 4)
	{price = 2.59;}
}

if (code == 1118) 
{
	if (size == 1)
	{price = 0.99;}

	else if (size == 2)
	{price = 1.59;}

	else if (size == 3)
	{price = 1.99;}

	else if (size == 4)
	{price = 2.99;}
}

if (code == 1119)
{ 
	cout << "Price is $7.99 in bulk of 50 fish." << endl;
	{price = 7.99;}
}

if (code == 1120)
{
	cout << "Price is $5.99 in bulk of 100 fish." << endl;
	{price = 5.99;}
}

if (code == 1121)
{
	cout << "Price is $1.29 per fish." << endl;
	{price = 1.29;}

}



cout << "Your price is $" << price << " each" << endl;
cout << "Please enter the quantity." << endl;
cin >> quantity;

cout << "Would you like to continue shopping or checkout?" << endl;
cout << "Please type (continue) or (checkout)" << endl;
cin >> response;

while (response == "continue")
{
cout << "Please enter the Item code to purchase a species of fish." << endl;
cin >> code;

if (code == 1111) 
	cout << "You have selected Bluegill." << endl;
else if (code == 1112)
	cout << "You have selected Redear Sunfish." << endl;
else if (code == 1113)
	cout << "You have selected Green Sunfish." << endl;
else if (code == 1114)
	cout << "You have selected Bluegill Hybrid." << endl;
else if (code == 1115) 
	cout << "You have selected Yellow Perch." << endl;
else if (code == 1116) 
	cout << "You have selected Channel Catfish." << endl;
else if (code == 1117)
	cout << "You have selected Largemouth Bass." << endl;
else if (code == 1118)
	cout << "You have selected Smallmouth Bass." << endl;
else if (code == 1119)
	cout << "You have selected Golden Shiner." << endl;
else if (code == 1120)
	cout << "You have selected Fathead Minnow." << endl;
else if (code == 1121)
	cout << "You have selected Koi Goldfish." << endl;

cout << "Please enter a size number (1-4)" << endl;
cin >> size;

if (code == 1111) 
{
	if (size == 1)
	{price = 0.79;}

	else if (size == 2)
	{price = 0.99;}

	else if (size == 3)
	{price = 1.39;}
}

if (code == 1112) 
{
	if (size == 1)
	{price = 0.85;}

	else if (size == 2)
	{price = 1.09;}

	else if (size == 3)
	{price = 1.69;}
}

if (code == 1113) 
{
	if (size == 1)
	{price = 0.85;}

	else if (size == 2)
	{price = 1.19;}

	else if (size == 3)
	{price = 1.79;}
}

if (code == 1114)
{
	if (size == 1)
	{price = 0.85;}

	else if (size == 2)
	{price = 1.09;}

	else if (size == 3)
	{price = 1.49;}
}

if (code == 1115)
{
	if (size == 1)
	{price = 0.99;}

	else if (size == 2)
	{price = 1.25;}

	else if (size == 3)
	{price = 1.99;}
}

if (code == 1116)
{
	if (size == 1)
	{price = 0.79;}

	else if (size == 2)
	{price = 1.10;}

	else if (size == 3)
	{price = 1.59;}

	else if (size == 4)
	{price = 2.30;}
}

if (code == 1117)
{
	if (size == 1)
	{price = 0.89;}

	else if (size == 2)
	{price = 1.19;}

	else if (size == 3)
	{price = 1.69;}

	else if (size == 4)
	{price = 2.59;}
}

if (code == 1118) 
{
	if (size == 1)
	{price = 0.99;}

	else if (size == 2)
	{price = 1.59;}

	else if (size == 3)
	{price = 1.99;}

	else if (size == 4)
	{price = 2.99;}
}

if (code == 1119)
{ 
	cout << "Price is $7.99 in bulk of 50 fish." << endl;
	{price = 7.99;}
}

if (code == 1120)
{
	cout << "Price is $5.99 in bulk of 100 fish." << endl;
	{price = 5.99;}
}

if (code == 1121)
{
	cout << "Price is $1.29 per fish." << endl;
	{price = 1.29;}

}



cout << "Your price is $" << price << " each" << endl;
cout << "Please enter the quantity." << endl;
cin >> quantity;

cout << "Would you like to continue shopping or checkout?" << endl;
cout << "Please type (continue) or (checkout)" << endl;
cin >> response;
}
total = (price * quantity);

cout << "\n" << "The total price comes out to $" << total << endl;
cout << "\n" << "Thank you, and have a nice day!" << endl;

system ("pause");
return 0;

}
Hi,
Firstly, initialize your (total) with 0 ('cause it will start with some random value if left uninitialized) :

double total = 0;
1
2
3
4
5
6
cout << "Would you like to continue shopping or checkout?" << endl;
cout << "Please type (continue) or (checkout)" << endl;
cin >> response;
}
total += (price * quantity);
cout << "\n" << "The current total price comes out to $" << total << endl;


You may want to continue shopping for a number of times, so the total price adds up. So we use addition operation instead of assignment operation.

Additionally, to make this "continue shopping or checkout" happens, add a while loop. Here is the place you put it :

1
2
3
4
5
6
7
8
9
string response = "continue";

while(response == "continue")
{
system("cls"); // clear screen
cout << "Tom's Fish Stock" << endl;
cout << "Price per fish" << endl;

// (.....) 


And close the loop with a curved bracket (}) :

1
2
3
4
5
6
7
cout << "Please type (continue) or (checkout)" << endl;
cin >> response;

total = (price * quantity);
cout << "\n" << "The current total price comes out to $" << total << endl;

} // The place, the place 
Last edited on
I noticed you have some while-loop lines - but it would be better if you removed them :

1
2
3
4
5
6
7
8
9
cout << "Would you like to continue shopping or checkout?" << endl;
cout << "Please type (continue) or (checkout)" << endl;
cin >> response;
while (response == "continue")
{
cout << "Please enter the Item code to purchase a species of fish." << endl;
cin >> code;

// ..... 
Does that help you? :)
closed account 5a8Ym39o6,

Thank you! That seems to get my program going in the right direction with what I wanted!
The only concern I'm having though is that I want the user to enter either "continue" or "checkout" as the only two options. As it is now, any response other than "continue" will stop the loop and process the program to end. How can I flag the user if he doesn't enter "checkout" when the user actually checks out? For example if the user enters "no" it does the same thing as checking out. I want only typing "checkout" to actually check out the total purchase. Any other input such as "no" I want the program to flag something like: "Sorry! Please either continue shopping or checkout." Any help will be appreciated.

Thanks,

MisterTams
> I want the program to flag something like: "Sorry! Please either continue shopping or checkout."

Use another while-loop to wrap the customer's decision :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
total = (price * quantity);
cout << "\n" << "The current total price comes out to $" << total << endl;

while(1)
{
cout << "Please type (continue) or (checkout) : ";

getline(cin, response);
if(response == "continue shopping") response = "continue";

if(response != "continue" && response != "checkout")
{
    cout << "Sorry! Please either 'continue shopping' or 'checkout'.\n\n"; continue;
}
break;
}
Last edited on
Does that help? :)
closed account 5a8Ym39o6,

Thank you very much! I haven't made the change on my program yet, but I totally see what the while loop will accomplish for flagging the user's input. The only other question I have is this: does (1) have to be next to while? Or could the parenthesis possibly be empty? Is (1) simply there to tell the program to run the while loop without a specific term such as used at the beginning of the program with while(response == "continue")?
while(1) is a way to make a program loop forever. Your program continues like that until the loop is terminated by the keywords break or return you write inside the white-loop body.
Hi,

I notice that your code has a lot of repetition, if that is the case then there is always a better way.

There are basically 3 components: the fish species code, it's size and the quantity. So this could implemented using a 2d array - the row is the species, the column is the size with a cell being the unit price. That way you can get the 3 pieces of info at the start, look up the appropriate unit price and then calculate the total.

It's a bit more complicated, but I think a lot better.

Hope this helps :+)
TheIdeasMan,

Thank you for your input! I'm actually a bit interested in trying this if it actually works and would make my code look cleaner. It's been a good while since I've used arrays. I never recall using arrays and then additional cells. Could you possibly show an example using a 2d array for maybe 2-3 fish so I could get the idea down? I would greatly appreciate it. :)

Thanks
So your problem is solved now? Or you are asking for (extra) advice I think? :)
closed account 5a8Ym39o6,

Yes my main question/concern has been answered by you! Thanks for that! :)
I'm just asking for advice on a different way of coding the program using 2d arrays to get the same effect. :)
Hi,

Sorry for the late reply, I was busy doing other things yesterday :+)

A simple 2d array of doubles with 3 rows and 3 columns looks like this:

1
2
3
4
5
double FishPrices[3][3] = {
                                         {0.79,0.99,1.39},
                                         {0.85,1.09,1.69},
                                         {0.85,1.19,1.79}        
                                         };

To refer to the second row, second column: std::cout << FishPrices[1][1] remembering that array indices start at zero.

However a better approach may be to have a vector of structs.

1
2
3
4
5
6
7
8
9
10
11
struct FishPrice {
    std::string Species;
    double SizePrices[3];

};

std::vector<FishPrice> FishPriceTable  = {
                                         {"Bluegill", 0.79, 0.99, 1.39},
                                         {"Redear Sunfish", 0.85,1.09,1.69},
                                         {"Green Sunfish ", 0.85, 1.19, 1.79}        
                                         };


To refer to the largest sized "Redear Sunfish" :

std::cout << FishPriceTable[1].SizePrices[2];

In a similar fashion, refer to the species name with: FishPriceTable[1].Species;

So you could have a menu which returns a number which relates to the species - 1 in this case. There could also be a user input for the size - 2 in this case. Then you can look up the price and multiply by the quantity to arrive at a subtotal.

Remember that to make use of std::string and std::vector one has to include their header files:

1
2
#include <string>
#include <vector> 


Also, make use of the reference material on this site - look at the links at the top left of this page.

Hopefully you can code the whole thing in 100 Lines Of Code (LOC) :+)



Topic archived. No new replies allowed.