### String Compare

This programm is asking the employee to inter her or his job category
e.g"Technical Staff" or "Administration Staff" then the program should ask the years the employee has worked if the employee entered a "Technical Staff" category then the program should calculate the salary increase else it should further as the user the exact department that the employee is in e.g Marketing or Accounting then calculate the salary.

I have created this code

cout<<"What type of staff are you? ";

cin>>category;
getline(cin,category,'\n');
cin>>salary;

if(category=="Technical Staff")
{
cout<<"How may years have you worked here? ";

cin>>years;

if(years>10)

salary=salary+salary*0.05;
else
salary=salary+salary*0.03;
}

{
cout<<"Which department are you working for? ";

cin>>department;
getline(cin,department,'\n');

if(department=="Account Department")
salary=salary+salary*0.06;

else if(department=="Marketing Department")
salary=salary+salary*0.04;

else
salary=salary+salary*0.03;
}

else
salary=salary+salary*0.02;

cout<<"You are a "<<category<<endl;
cout<<"You have worked for "<<years<<endl;

cout<<"you are in "<<department<<endl;
cout.setf(ios::fixed);
cout.precision(2);

return 0;
}

could you please help; after entering the salary the program ignores all the ifs and go straight to the end of the program....
Last edited on
Brackets are incorrect. For example the first if has an open bracket and closes after the second. Write if statements like this

If(condition){

//code

}else{

//code

};

And nested ifs should look like this with the first if wrapping the second:

If(condition){
//CODE

If(condition){
//code
}else{

};
}:

Indent each if for clarity. Bracket each else for clarity. Idk if you need them around else but it helps.

Follow these guidelines and you can fix it
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include"stdafx.h" #include #include using namespace std; int main() { string category,department; int years; double salary; cout<<"What type of staff are you? "; getline(cin,category,'\n'); cout<<"Enter your current salary: "; cin>>salary; cout<<"How may years have you worked here? "; cin>>years; cout<<"Which department are you working for? "; cin.ignore(); getline(cin,department,'\n'); if(category=="Technical Staff") { if(years>10) salary=salary+salary*0.05; else salary=salary+salary*0.03; } else if (category=="Administrative Staff") { if(department=="Account Department") salary=salary+salary*0.06; else if(department=="Marketing Department") salary=salary+salary*0.04; else salary=salary+salary*0.03; } else salary=salary+salary*0.02; cout<<"You are a "<
You sure that solves the skipping if's? Logically its better but it seems to me that it would still skip. If not elaborate on why please.
The logic is killing me here, wow @Akshit I wish i could be you!!
@LovestoCpp thanks i have seen those errors.
I doesn't found any hard error with your code.I only got error that years in not initialised.

So I moved those statements up.

It worked fine and program is working greatly.
you have to use string data type

 ``` string category,department; double salary,years; cout<<"What type of staff are you? "; getline(cin,category,'\n'); cout<<"Enter your current salary: "; cin>>salary;```

similarly for department use getline(....)
@hombakajiz your welcome. I may just be a neat freak if what Akshit says is true lol either way let us know how it works out.
I just started using VC++ from tomorrow.Untill then I was using Turbo c++.
So if any error occur tell me so I can test it in Turbo c++.and improve my VC++ experience.
@LovestoCpp its working..
Last edited on
Topic archived. No new replies allowed.