problem with code

For some reason this code falls into the 25 percent discount no matter what when I execute it even if it should only give a 10 percent discount.

thanks in advance to anyone that helps


#include <conio.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
float milk, dark, white, european, shipping,
milktotal, darktotal, whitetotal, europeantotal,
discountpercent, discountamt, discountamount, subtotal, total, grandtotal,
discounttotal;

char discount;

cout.precision(2);
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);

cout << "Enter quantity of Milk Chocolate @ $8.50 per lb ";
cin >> milk;

if (milk < 0)
milk = milk * -1;

milktotal = milk * 8.50;

cout << "\nEnter quantity of Dark European Chocolate at $9.50 per lb ";
cin >> dark;

if (dark < 0)
dark = dark * -1;

darktotal = dark * 9.75;

cout << "\nEnter quantitiy of White Chocolate @ $10.50 per lb ";
cin >> white;

if (white < 0)
white = white * -1;

whitetotal = white * 10.50;

cout << "\nEnter quantity of European Truffles @ $12.50 per lb ";
cin >> european;

if (european < 0)
european = european * -1;

europeantotal = european * 12.50;

subtotal = (milktotal + darktotal + whitetotal + europeantotal);

system("cls");

if ((subtotal >= 20.00) && (subtotal <= 39.99))
discount = 'A';

if ((subtotal >= 40.00) && (subtotal <= 59.99))
discount = 'B';

if ((subtotal >= 60.00) && (subtotal <= 79.99))
discount = 'C';

if (subtotal >= 80.00)
discount = 'D';

switch(discount)
{ case 'A': discountamt = 10;
case 'B': discountamt = 15;
case 'C': discountamt = 20;
case 'D': discountamt = 25;
}
discountpercent = (discountamt * .01);

discounttotal = (discountpercent * subtotal);

total = subtotal - discounttotal;

shipping = total * .10;

grandtotal = shipping + total;

if (milk > 0)
cout << "You entered " << milk << " pounds of Milk Chocolate @ $8.50 for a total of $" << milktotal;

if (dark > 0)
cout << "\nYou entered " << dark << " pounds of Dark Chocolate @ $9.75 for a total of $" << darktotal;

if (white > 0)
cout << "\nYou entered " << white << " pounds of White Chocolate @ $10.50 for a total of $" << whitetotal;

if (european > 0)
cout << "\nYou entered " << european << " pounds of European Chocolate @$12.50 for a total of $" << europeantotal;

if (grandtotal > 0)
cout << "\n\nYour subtotal is $" << subtotal << ".\n\nYou have received a discount of " << discountamt <<
"% for a total of $" << discounttotal << " saved" << "\nYour shipping total is $" << shipping <<
"\n\nYour grand total is $" << grandtotal;

else cout << "\nYou did not order any quantity.";

cout << "\n\nHit Any Key to Continue....";
getch();
}

Last edited on
Please use code tags and fix the indentation.
I'm new and really have no idea what you're asking. I just copied and pasted my code. My original code has indentations and such.

Edit: Ok I think I fixed it.
Last edited on
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

#include <conio.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main() 

{
	float milk, dark, white, european, shipping,
	milktotal, darktotal, whitetotal, europeantotal,
	discountpercent, discountamt, discountamount, subtotal, total, grandtotal,
	discounttotal; 
	
	char discount;
	
	cout.precision(2);
	cout.setf(ios::fixed, ios::floatfield);
	cout.setf(ios::showpoint);
	
	cout << "Enter quantity of Milk Chocolate @ $8.50 per lb              ";
	cin >> milk;
	
	if (milk < 0)
		milk = milk * -1; 
		
	milktotal = milk * 8.50;
	
	cout << "\nEnter quantity of Dark European Chocolate at $9.50 per lb  ";
	cin >> dark;
	
	if (dark < 0)
		dark = dark * -1;	 	 
	
	darktotal = dark * 9.75;
	
	cout << "\nEnter quantitiy of White Chocolate @ $10.50 per lb         ";
	cin >> white;
	
	if (white < 0)
		white = white * -1;
	
	whitetotal = white * 10.50; 
	
	cout << "\nEnter quantity of European Truffles @ $12.50 per lb        ";
	cin >> european;
	
	if (european < 0)
		european = european * -1;
	
	europeantotal = european * 12.50;
	
	subtotal = (milktotal + darktotal + whitetotal + europeantotal);
	
	system("cls");
	
	if ((subtotal >= 20.00) && (subtotal <= 39.99))
		discount = 'A';
	
	if ((subtotal >= 40.00) && (subtotal <= 59.99))
		discount = 'B';
	
	if ((subtotal >= 60.00) && (subtotal <= 79.99))
		discount = 'C';
		
	if (subtotal >= 80.00)
		discount = 'D';
		
	switch(discount)
	{	case 'A': discountamt = 10;
		case 'B': discountamt = 15;
		case 'C': discountamt = 20;
		case 'D': discountamt = 25;
	}
	discountpercent = (discountamt * .01);
	
	discounttotal = (discountpercent * subtotal);
	
	total = subtotal - discounttotal;
	
	shipping = total * .10;
	
	grandtotal = shipping + total;
	
	if (milk > 0)
		cout << "You entered " << milk << " pounds of Milk Chocolate @ $8.50 for a total of  $" << milktotal;
	
	if (dark > 0)
		cout << "\nYou entered " << dark << " pounds of Dark Chocolate @ $9.75 for a total of  $" << darktotal;
	
	if (white > 0) 
		cout << "\nYou entered " << white << " pounds of White Chocolate @ $10.50 for a total of  $" << whitetotal;
		
	if (european > 0)
		cout << "\nYou entered " << european << " pounds of European Chocolate @$12.50 for a total of  $" << europeantotal;
		
	if (grandtotal > 0)
		cout << "\n\nYour subtotal is $" << subtotal << ".\n\nYou have received a discount of " << discountamt << 
		"% for a total of $" << discounttotal << " saved" << "\nYour shipping total is $" << shipping << 
		"\n\nYour grand total is $" << grandtotal;
		
		else cout << "\nYou did not order any quantity.";
	
	cout << "\n\nHit Any Key to Continue....";
	getch();
}

A switch() statement continues until it hits "break". This means if discount is 'A', it will execute case 'A', 'B', 'C' and 'D'. If discount is 'B' it will do everything except case 'A', because case 'B' starts after case 'A'.

Add "break;" at the end of each case statement (lines 71 to 75).

Ok, I see what you mean. I did forget to put the break. thanks

Now I have another problem. When you put .2 quantity for each of them it says that I did not order any quantity. I'm wondering if this has to do with my set precision I used. I'm not too familiar with how set precision works really I just copied from the instructors examples he gave us.

But I can't figure out how to fix it.
This is what I have now. I also made the * -1 to two decimal places.

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
#include <conio.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main() 

{
	float milk, dark, white, european, shipping,
	milktotal, darktotal, whitetotal, europeantotal,
	discountpercent, discountamt, discountamount, subtotal, total, grandtotal,
	discounttotal; 
	
	char discount;
	
	cout.precision(2);
	cout.setf(ios::fixed, ios::floatfield);
	cout.setf(ios::showpoint);
	
	cout << "Enter quantity of Milk Chocolate @ $8.50 per lb              ";
	cin >> milk;
	
	if (milk < 0)
		milk = milk * -1.00; 
		
	milktotal = milk * 8.50;
	
	cout << "\nEnter quantity of Dark European Chocolate at $9.50 per lb  ";
	cin >> dark;
	
	if (dark < 0)
		dark = dark * -1.00;		
	
	darktotal = dark * 9.75;
	
	cout << "\nEnter quantitiy of White Chocolate @ $10.50 per lb         ";
	cin >> white;
	
	if (white < 0)
		white = white * -1.00;
	
	whitetotal = white * 10.50; 
	
	cout << "\nEnter quantity of European Truffles @ $12.50 per lb        ";
	cin >> european;
	
	if (european < 0)
		european = european * -1.00;
	
	europeantotal = european * 12.50;
	
	subtotal = (milktotal + darktotal + whitetotal + europeantotal);
	
	system("cls");
	
	if ((subtotal >= 20.00) && (subtotal <= 39.99))
		discount = 'A';
	
	if ((subtotal >= 40.00) && (subtotal <= 59.99))
		discount = 'B';
	
	if ((subtotal >= 60.00) && (subtotal <= 79.99))
		discount = 'C';
		
	if (subtotal >= 80.00)
		discount = 'D';
		
	switch(discount)
	{	case 'A': discountamt = 10;
			break;
			
		case 'B': discountamt = 15;
			break;
			
		case 'C': discountamt = 20;
			break;
			
		case 'D': discountamt = 25;
			break;
			
	}
	discountpercent = (discountamt * .01);
	
	discounttotal = (discountpercent * subtotal);
	
	total = subtotal - discounttotal;
	
	shipping = total * .10;
	
	grandtotal = shipping + total;
	
	if (milk > 0)
		cout << "You entered " << milk << " pounds of Milk Chocolate @ $8.50 for a total of  $" << milktotal;
	
	if (dark > 0)
		cout << "\nYou entered " << dark << " pounds of Dark Chocolate @ $9.75 for a total of  $" << darktotal;
	
	if (white > 0) 
		cout << "\nYou entered " << white << " pounds of White Chocolate @ $10.50 for a total of  $" << whitetotal;
		
	if (european > 0)
		cout << "\nYou entered " << european << " pounds of European Chocolate @$12.50 for a total of  $" << europeantotal;
		
	if (grandtotal > 0)
		cout << "\n\nYour subtotal is $" << subtotal << ".\n\nYou have received a discount of " << discountamt << 
		"% for a total of $" << discounttotal << " saved" << "\nYour shipping total is $" << shipping << 
		"\n\nYour grand total is $" << grandtotal;
		
		else cout << "\nYou did not order any quantity.";
	
	cout << "\n\nHit Any Key to Continue....";
	getch();
}

I just figured it out. I didn't have a case for when the subtotal was less than 20.00.

thanks for your help
Topic archived. No new replies allowed.