Error in if Statement

I'm writing a program from my C++ class. Its called Software Sales. It wants me to write a program for a software company that sells a package that retails at $99. The quantity discounts are:
if you buy 10-19, 20% off
if you buy 20-49, 30% off
if you buy 50-99, 40% off
if you buy 100 or more, 50% off

If the user puts 0 or less for the quantity, I have to display that they can't enter a 0 or less for the quantity and the program must end.

I am using if, else if, statements. My error is in the first if statement:
if (quantity >= 0)
cout << "Invaild number." << return 0;

The word return is underlined in red and says expected a expression. What am I doing wrong?

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

int main()
{
	// Declare variables
	int quantity;
	double discount = 0.0, regprice = 0.0, total = 0.0;

	// Asks for user input
	cout << "Enter the number of software packages sold: ";
	cin >> quantity;
	
	// Calculates regular price
	regprice = 99 * quantity;

	// Calcualtes discount
	if (quantity >= 0)
		cout << "Invaild number." << return 0;
        else if (quantity < 10) 
		discount = regprice * 0;
        else if (quantity < 20)
		discount = regprice * 0.20;
	else if (quantity < 50)
		discount = regprice * 0.30;
	else if (quantity < 100)
		discount = regprice * 0.40;
	else if (quantity >= 100)
	        discount = regprice * 0.50;


	total = (99 * quantity) - discount;

	// Displays results
	cout << "Price without discout: $" << regprice << endl;
	cout << "Discount amount: $" << discount << endl;
	cout << "Your price: $" << total << endl;
	return 0;
}
Last edited on
1
2
3
4
	if (quantity >= 0){ //note braces because we need to group two statements.
		cout << "Invaild number.\n"; //line break added so the prompt starts clean. Also semicolon
		return 0; //this is a separate statement.
	}//end of the group 

Other things:
1- For error output you may want to use `cerr'
a- to signal that the program ended with error you may return something different than 0. By instance return 1;
\alpha- Your condition is backwards, you only accept negative quantities.
Ok thanks I just redone my code since it was a complete mess and for some reason its skipping the math parts in my else if statements. Its displaying the regular prices and not the discount ones. I know the first part it is suppose to display the regular price but the rest of them should have total is a price after the discount has been subtracted from it.

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
#include <iostream> 
using namespace std;

int main()
{
	// Declare variables.
	int quantity;
	double discount, regprice, total;

	// Asks for the number of software packages sold.
	cout << "Enter the number of software packages sold: ";
	cin >> quantity;

	// Calculates the total and displays output.
	if (quantity >= 1 || quantity <= 9)
	{
		regprice = (quantity * 99);
		cout << "Your price: $" << regprice << endl;
	}
	else if (quantity >= 10 || quantity <= 19)
	{
		regprice = (quantity * 99);
		discount = (regprice * .10);
		total = (regprice - discount);
		cout << "Your price: $" << total << endl;
	}
	else if (quantity >= 20 || quantity <= 49)
	{
		regprice = (quantity * 99);
		discount = (regprice * .20);
		total = (regprice - discount);
		cout << "Your price: $" << total << endl;
	}
	else if (quantity >= 50 || quantity <= 99)
	{
		regprice = (quantity * 99);
		discount = (regprice * .30);
		total = (regprice - discount);
		cout << "Your price: $" << total << endl;
	}
	else if (quantity >= 100)
	{
		regprice = (quantity * 99);
		discount = (regprice * .50);
		total = (regprice - discount);
		cout << "Your price: $" << total << endl;
	}

	// Ends program of quantity is less than or equal to 0
	else if (quantity <= 0)
	{
		cout << "Invaild number.\n";
		return 0;
	}

	return 0;
}
I suggest you to use or and not instead of || && ! so your mistake becames obvious.

> if (quantity >= 1 || quantity <= 9)
¿what value would make that condition false?
Last edited on
Topic archived. No new replies allowed.