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
#include <iostream>
usingnamespace 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. ";
}
elseif (2000<=salary<=3499){
cout<< "\n Good Job! ";
}
elseif (1000<=salary<1999){
cout<< "\n It’s Nice but you can find another job. ";
}
elseif (0<salary<1000) {
cout<< "\n You should really consider finding another job.";
}
return 0;
}
#include <iostream>
usingnamespace 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. ";
}
elseif (salary >= 2000 && salary < 3500) {
cout << "\n Good Job! ";
}
elseif (salary >= 1000 && salary < 2000) {
cout << "\n It’s Nice but you can find another job. ";
}
elseif (salary > 0 && salary <1000) {
cout << "\n You should really consider finding another job.";
}
return 0;
}
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: elseif (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