Math operator fails

Dear programmers,
I'm writing a program that asks for the number of items ordered as input and the price as output (considering variable discounts). The user cannot input 0; if he/she does, an error message is shown. But in my code when 0 is entered and Enter is presses, the program shows error for debugging. Can you help me fix this problem? It works for any number larger than 1, but not 0. The code is below:
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
  #include <iostream>
using namespace std;

int main()
{
	double retailPrice = 99, discount, subTotal;
	int unitsBuy;

	cout << "How many units do you want to purchase?\n";
	cin >> unitsBuy;

	if (unitsBuy < 1) //This line does not work.
		cout << "Please enter a value larger than 1.\n";
	
	
	else if (unitsBuy >= 1 && unitsBuy < 10)
			discount = 0.00;
		else if (unitsBuy >= 10 && unitsBuy <= 19)
			discount = 0.20;
		else if (unitsBuy >= 20 && unitsBuy <= 49)
			discount = 0.30;
		else if (unitsBuy >= 50 && unitsBuy <= 99)
			discount = 0.40;
		else if (unitsBuy >= 100)
			discount = 0.50;
	

	subTotal = unitsBuy * (retailPrice - (retailPrice*discount));

	cout << "You ordered a total of " << unitsBuy << " for which you get " << discount << " discount." <<
		"\nYour total subtotal is $" << subTotal << endl;
	return 0;
}
Last edited on
What do you mean by 'shows error for debugging'?

I don't see any problem with line 12.
When I enter 0 in the console, VSudio shows a message that do you want to Abort, try, or ignore. This message comes before line 13 executes. But when I press Abort button, line 13 executes.
closed account (48bpfSEw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14


int main()
{
...

	if (unitsBuy < 1)  { //This line does not work.
		cout << "Please enter a value larger than 1.\n";

	        return; // exit programm
          }
	...
}
When I enter 0 in the console, VSudio shows a message that do you want to Abort, try, or ignore.
The code itself does not cause this behavior. The problem must be somewhere else.

What you have are several uninitialized variables that show garabage values.
Abe Khademi wrote:
When I enter 0 in the console, VSudio shows a message that do you want to Abort, try, or ignore. This message comes before line 13 executes.

How do you know? Are you using a debugger?

Note that cout is buffered so the output is not guaranteed to be shown right away. It could very well be the case that line 13 is executed, and then the error occur, and not until the program is about to be shut down the message is printed. Use std::flush or std::endl if you want it to show up right away.

 
cout << "Please enter a value larger than 1." << endl;
Last edited on
Two problems:

When I enter 0 in the console, VSudio shows a message that do you want to Abort, try, or ignore. This message comes before line 13 executes.

Line 13 executes. However, as Peter87 mentioned, you're using \n instead of endl so the line is not displayed until cout is flushed.

Second problem. If unitsBuy is 0, you continue on to line 28 as if nothing had happened. By jumping directly to line 28, discount is never set. Your debugger is detecting an attempt to use an uninitialized variable in line 28.
Last edited on
Thanks all for your valuable input and help. I tried what Necip suggested and it worked:

1
2
3
4
if (unitsBuy < 1) {
		cout << "Please enter a value larger than 1.\n";
		return 0;
		}


I think in this way, the second problem that AbstractionAnon mentioned is solved because the program exits if the condition (<1) is met.
Last edited on
Topic archived. No new replies allowed.