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');
cout<<"Enter your current salary: ";
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;
}


else if (category=="Administrative Staff")
{
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);
cout<<"Your salary is R "<<salary<<endl;

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
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include"stdafx.h"
#include<iostream>
#include<string>
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 "<<category<<endl;
cout<<"You have worked for "<<years<<endl;


cout<<"you are in "<<department<<endl;
cout.setf(ios::fixed);
cout.precision(2);
cout<<"Your salary is R "<<salary<<endl;
system("pause");

return 0;
}
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 edited your code only.
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.
Please next time be careful mention your variable type..
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.