If Else Problem

Hello All,
I am having problem in C++ programming in If Else statements. I have typed everything in the program and it is compiling as well, but the problem is when the salary is (1000<=salary<1999 and (0<salary<1000) the following statement should be given "It’s Nice but you can find another job. " and "You should really consider finding another job.". But i am getting this statement "Good Job! "
Please Help


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
#include <iostream>
using namespace std;
int main ()
{
	int h1, h2;
	int salary;
	
	cout<< "Please enter how many hours you have worked: ";
	cin>> h1;
	
	cout<< "Please enter how much you are getting paid for per hour: ";
	cin>> h2;
	
	salary = h1*h2;
	
	cout << "\n Total Money  = " <<salary;
	
	if (salary>=3500){
		cout<< "\n You earn good dude. ";
	}
	
	else if (2000<=salary<=3499){
		cout<< "\n Good Job! ";
	}
	
	else if (1000<=salary<1999){
		cout<< "\n It’s Nice but you can find another job. ";
	}
	
	else if (0<salary<1000) {
		cout<< "\n You should really consider finding another job.";
	}
	return 0;
}
Last edited on
The code wasn't exactly correct. The corrections should be fairly obvious.
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
#include <iostream>
using namespace std;
int main()
{
	int h1, h2;
	int salary;

	cout << "Please enter how many hours you have worked: ";
	cin >> h1;

	cout << "Please enter how much you are getting paid for per hour: ";
	cin >> h2;

	salary = h1 * h2;

	cout << "\n Total Money = " << salary;

	if (salary >= 3500) {
		cout << "\n You earn good dude. ";
	}

	else if (salary >= 2000 && salary < 3500) {
		cout << "\n Good Job! ";
	}

	else if (salary >= 1000 && salary < 2000) {
		cout << "\n It’s Nice but you can find another job. ";
	}

	else if (salary > 0 && salary <1000) {
		cout << "\n You should really consider finding another job.";
	}
	return 0;
}
Hello HS05669,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

The problem is not the else if statement, but the way you write it,1000<=salary<1999 may be nice to explain what needs to be done, but you can not just put this in a condition. The if statement condition needs to be written as: else if (1000 <= salary && salary < 1999). If there is more than one condition to deal with it needs to be seperated with && or ||, logical and or logical or. The same concept will need to be used for the other two else if statements.
See http://www.cplusplus.com/doc/tutorial/operators/#logical

Give it a try.

Hope that helps,

Andy
Thank You so much!! This solved my entire problem
i cant thank you enough
Welcome also if you wish to learn C++ from beginner level to a more advanced level why not check out this udemy course: https://www.udemy.com/learn-cpp-from-scratch/?couponCode=CPPFORUM
Topic archived. No new replies allowed.