What's the if condition to limit a number?

So our instructor gave as an exercise where a user inputs a number and if the input number is greater than 1000, it'll compute this. But, there's a maximum number which is 3200, if it exceeds that number, it shouldn't compute the formula..
Here's the problem:

SALE VALUE Commission
up to 100 --- 0
over 100-1000 -- 2 %
over 1000 -- 3%
maximum is 3200

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream.h>
int main ()

{
	float c, sv;

	cout <<"Enter sale value:";
	cin >> sv;

	if (sv <= 100)
		c= 0;

	else if (sv >= 100)
		c = sv * .02;

	else if (sv > 1000 && sv < 3200)
		c = sv *.03;

	cout <<"" << endl;
	cout <<"Your commision is " << c << endl;

	return 0;

}

My instructor said that it's wrong since when I entered 3201 and 3202, the results were different in which it should be different.
But I used the logical &&. What might be the problem?
Last edited on
I'm not exactly sure what your problem i but i hope i solved 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
 #include <iostream>

using namespace std;

int main ()

{
	float c, sv;

	cout <<"Enter sale value:";
	cin >> sv;

    if (sv <= 3200 && sv > 0){
        if (sv <= 100)
            c= 0;

        else if (sv >= 100)
            c = sv * .02;

        else if (sv > 1000 && sv <= 3200)
            c = sv *.03;

        cout <<"" << endl;
        cout <<"Your commision is " << c << endl;
    }
	return 0;

}
Last edited on
When I enter anything greater than 3200, there is no result.
up to 100               0 < sv <=100
over 100-1000           100 < sv <=1000
over 1000              1000 < sv <=3200
maximum is 3200


But you write:
1
2
3
if (sv <= 100)
else if (sv >= 100)
else if (sv > 1000 && sv < 3200) //this will never execute 
Last edited on
I have fixed your problem. Now if you input number greater than 3200. It will display message and for number less than 3200 you will have correct output.

Modified code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main (){
	float c, sv ;
	cout <<"Enter sale value:";
	cin >> sv;

	if (sv <= 3200 && sv > 0){
		if (sv <= 100)
			c= 0;
		else if (sv > 100 && sv <= 1000 )
			c = sv * .02;
		else if (sv > 1000 && sv <= 3200)
			c = sv *.03;
		cout <<"" << endl;
		cout <<"Your commision is " << c << endl;
	} else {
		cout <<"Number is greater than 3200 " <<endl;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.