How/Which Loop To use?

So I have a problem with looping. Not sure which one to use for the assignment I got. I wrote the code but haven't trouble looping to the scenario it asks for.

Here is the assignment:

Assignment: (Calculating Total Sales) A bakery sells five different products whose retail prices are:
product 1 — $2.47, product 2—$4.42, product 3—$9.98, product 4—$4.49 and product 5— $6.87.

Write a program that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold

1. Your program should use a switch statement to determine the retail price for each product.
2. Your program should calculate and display the total retail value of all products sold.

3. Use a loop to determine when the program should stop looping and display the final results. When product number is - 1, your loop should stop and print the final results.

Having trouble at 3 where user inputs -1 and it would calculate total amount. I tried do while but no luck. Would really appreciate help, I looked at resources and tried but had no luck so hoping for help.


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

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

int main()
{
	double product1 = 2.47, product2 = 4.40, product3 = 9.98, product4 = 4.49, product5 = 6.87;
	double productcount = 0, total1 = 0, total2 = 0, total3 = 0, total4 = 0, total5 = 0;
	int productnum = 0;

	//for (; productnum != -1;)
	//{
		cout << "What product number did you sell this week?" << endl;
		cin >> productnum;

		cout << "Quantity Sold?" << endl;
		cin >> productcount;



		switch (productnum)
		{

		case 1:
			total1 = (product1 * productcount);
			break;
		case 2:
			total2 = (product1 * productcount);
			break;
		case 3:
			total3 = (product1 * productcount);
			break;
		case 4:
			total4 = (product1 * productcount);
			break;
		case 5:
			total5 = (product1 * productcount);
			break;
		}


	//}
		cout << "The total value of all products sold is: $ " << (total1 + total2 + total3 + total4 + total5) << endl;
	

	


	system("pause");
}

Hey =),

A simple while loop with a boolean condition should work perfectly fine -

1
2
3
4
5
6
7
8
9
bool run = true;
while(run) // runs as long as run equals true
{
    // switch statement...

    case -1: 
          run = false;
          break;
}


But a few other changes has to be made. The question about which product number was sold as well as the Quantity sold has to be inside the while loop, so they can be asked each iteration.

One thing about your calculations, if I first pick product 1, everything works fine. If I pick product 1 again with different quantities sold, it will overwrite the previous selling. You don't want that, you want to add it all up together. That's why you need to change from this -

total1 = (product1 * productcount);
to this -
total1 = total1 + (product1 * productcount); // a shorter way would be total1 += (product1 * productcount);

And you'll need to place the quantity question inside a if statement which becomes true only if productnum is not equal to -1. This way, if I decide to enter -1 to quit the program, it won't ask me about the quantity sold, that would be a bit nonsensical.

Another mistake is that you are using product1 to calculate for all products, which you obviously don't want, each product has it's own price that you want to use.

Full program:

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 <iomanip>
using namespace std;

int main()
{
	double product1 = 2.47, product2 = 4.40, product3 = 9.98, product4 = 4.49, product5 = 6.87;
	double productcount = 0, total1 = 0, total2 = 0, total3 = 0, total4 = 0, total5 = 0;
	int productnum = 0;

	bool run = true;
	while (run)
	{
		cout << "What product number did you sell this week?" << endl;
		cin >> productnum;

		if (productnum != -1)
		{
			cout << "Quantity Sold?" << endl;
			cin >> productcount;
		}
		
		switch (productnum)
		{

		case -1: 
			run = false;
			break;
		case 1:
			total1 = total1 + (product1 * productcount);
			break;
		case 2:
			total2 = total2 + (product2 * productcount);
			break;
		case 3:
			total3 = total3 + (product3 * productcount);
			break;
		case 4:
			total4 = total4 + (product4 * productcount);
			break;
		case 5:
			total5 = total5 + (product5 * productcount);
			break;
		}
	}
	cout << "The total value of all products sold is: $ " << (total1 + total2 + total3 + total4 + total5) << endl;
	system("pause");
}
Last edited on
Wow fast response. Thank you so much for the help. The program runs perfectly.

I didn't even think to use the bool since I didn't know much about it. I forgot the variables value get replaced after every loop.

Once again thanks for your help and quick response.
You're welcome =)
Topic archived. No new replies allowed.