varable in if statement

What am i doing wrong? I cannot get the discount_percent variable to apply inside of each if statement. it either says 0 or 100 when it is compiled
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
 #include <iostream>
#include <iomanip>

using namespace std;
int main() 
{
	//constant regular price variable
	const double REGULAR_PRICE = 99.0;
	
	//variables 
	int units_sold, pre_discount_amount, savings, discount_percent;
	
	//get user input of number of units 
	cout << "Number of units purchased is: ";
	cin >> units_sold;
	
	//Pre-discount amount 
	pre_discount_amount = units_sold * REGULAR_PRICE;
	
	cout << setprecision(2) << fixed;
	
	if (units_sold >= 0 && units_sold <=9)  
	{
		discount_percent = 0;
		savings = pre_discount_amount * discount_percent;
		cout <<"Number of units purchased is: " << units_sold
			 <<"\nDiscount applied is: " << discount_percent * 100 << "%" 
             <<"\nTotal savings due to discount is: " << savings * units_sold 
			 <<"\nTotal cost of the purchase is: " << (units_sold * REGULAR_PRICE) - savings << endl;
	}	
	else if (units_sold >= 10 && units_sold <=39) 	
	{
		discount_percent = .10;
		savings = pre_discount_amount * discount_percent;
		cout <<"Number of units purchased is: " << units_sold
			 <<"\nDiscount applied is: " << discount_percent * 100 << "%" 
             <<"\nTotal savings due to discount is: " << savings * units_sold 
			 <<"\nTotal cost of the purchase is: " << (units_sold * REGULAR_PRICE) - savings << endl;
	}
	else if (units_sold >= 40 && units_sold <=65) 	
	{
		discount_percent = .18;
		savings = pre_discount_amount * discount_percent;
		cout <<"Number of units purchased is: " << units_sold
			 <<"\nDiscount applied is: " << discount_percent * 100 << "%" 
             <<"\nTotal savings due to discount is: " << savings * units_sold 
			 <<"\nTotal cost of the purchase is: " << (units_sold * REGULAR_PRICE) - savings << endl;
	}
}
discount_percent is defined as an int. You try to assign values that are floating point values that are less than 1. 0.10 and 0.18 are integer truncated to 0.

If you declared discount_percent as either a float or double then the assigned values would work.
Last edited on
OMG thank you. I knew it was something frustratingly simple that I was overlooking. I appreciate it.
This can be simplified by moving the output code to after the discount tests:

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

using namespace std;

int main()
{
	//constant regular price variable
	const double REGULAR_PRICE {99.0};

	//variables
	int units_sold {};
	double discount_percent {};

	//get user input of number of units
	cout << "Number of units purchased is: ";
	cin >> units_sold;

	//Pre-discount amount
	const auto pre_discount_amount {units_sold * REGULAR_PRICE};

	cout << setprecision(2) << fixed;

	if (units_sold >= 10 && units_sold <= 39)
		discount_percent = .10;
	else if (units_sold >= 40 /* && units_sold <= 65*/)
		discount_percent = .18;

	const auto savings {pre_discount_amount * discount_percent};

	cout << "Number of units purchased is: " << units_sold
		<< "\nDiscount applied is: " << discount_percent * 100 << "%"
		<< "\nTotal savings due to discount is: " << savings
		<< "\nTotal cost of the purchase is: " << (units_sold * REGULAR_PRICE) - savings << '\n';
}



Number of units purchased is: 50
Number of units purchased is: 50
Discount applied is: 18.00%
Total savings due to discount is: 891.00
Total cost of the purchase is: 4059.00

Topic archived. No new replies allowed.