Help with simple program :(

I'm trying to have the user input the product number and quantity of each product. It isn't t working right, I'm not sure what order to put everything.
It only outputs the price of the last product in my function.


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

using namespace std;

int productPrice(int, int);

int main(int argc, char** argv) {
	
	int productNum, quantity;
	double price, total;
	char n;
	bool repeat = true;
	
	while (cin)
	{
		cout << "Please enter the product number (1-5).Enter n to quit ";
		cin >> productNum;
		cout << "Please enter the quantity. ";
		cin >> quantity;
		cout << "The price for " << quantity << " product number " << productNum << " is $" << productPrice(productNum, quantity) << endl;
	
		for (int i = 0; i <= 5; i++)
		{
			total = productPrice(productNum, quantity) + productPrice(productNum, quantity);
		}	
	
		if ((productNum == 'N') || (productNum == 'n'))
		{
			cout << "Your total price is $" << total << endl;
		}
				
		else 
		repeat = true;
		
		
		
		

	}
	
	
	return 0;
}


int productPrice(int productNum, int quantity)
{
	double price, prodPrice;
	
	switch (productNum)
		{
			case 1:
				prodPrice = 3.08;
                        break;
			case 2:
				prodPrice = 4.53;
                        break;
			case 3:
				prodPrice = 10.87;
                        break;
			case 4:
				prodPrice = 4.49;
                        break;
			case 5:
				prodPrice = 9.68;
                        break;
		}
		
		price = prodPrice * quantity;
	
	return price;
	
} 
Last edited on
Lines 52-61: Switch statements are designed in such a way that you can fall through the different cases, which makes making a set of cases do the same thing easy. Just below each prodPrice line, there needs to be a break to jump out of the switch statement, as you don't want that fall-through behavior.

-Albatross
Thank you, that helps a lot. Now how can I calculate the total price of all products and quantities entered? Should my calculations go outside the while loop? My total price is outputting $0.
I've taken mt calculations out of my loop (just to see if it works) I think it's better? However, when i enter n to quit it enters into an infinite loop. :(


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

using namespace std;

double productPrice(int, int);

int main(int argc, char** argv) {
	
	int productNum, quantity;
	double price, total;
	char n;
	bool repeat = true;
	
	while (cin)
	{
		cout << "Please enter the product number (1-5).Enter n to quit ";
		cin >> productNum;
	
		cout << "Please enter the quantity. ";
		cin >> quantity;
		cout << "The price for " << quantity << " product number " << productNum << " is $" << productPrice(productNum, quantity) << endl;

		
		if ((productNum == 'n') || (quantity == 'n'))
		{ 
			return 0;
		}
		
	}
	
	for (int i = 0; i <= quantity; i++)
		{
			total = productPrice(productNum, quantity) + productPrice(productNum, quantity);
		}	
		
		
	cout << "Your total price is $" << total << endl;
	
	
	return 0;
}


double productPrice(int productNum, int quantity)
{
	double price, prodPrice;
	
	switch (productNum)
		{
			case 1:
				prodPrice = 3.08;
				break;
			case 2:
				prodPrice = 4.53;
				break;
			case 3:
				prodPrice = 10.87;
				break;
			case 4:
				prodPrice = 4.49;
				break;
			case 5:
				prodPrice = 9.68;
				break;
		}
		
		price = prodPrice * quantity;
	
	return price;
	
}
Last edited on
Topic archived. No new replies allowed.