if statement and assignments

hello I was given an assignment to generate a payroll for employees using two arrays. one for employee details and one for the computations.senior members and junior members have different income tax and rent and transport allowances and so has to be computed based on the employees status.
I tried using an if statement but the else statement overides the assignment for the if. and the conditional dont seem to be working. what could be the cause
this is my code


#include<iostream>
#include<string>
#include<iomanip>

using namespace std;
int main()
{

cout<<"\t\t\t\t bussiness application developers association \n";
cout<<" \t\t\t\t\t\t payroll for month ending\n \t\t\t\t\t\tcopyright 10574224\n\n";
cout<<"employee id\t"<<"name\t"<<"gender\t"<<"department\t"<<"date engaged\t"
<<"status\t"<<"annual gross\t"<<"monthly basic\t"<< "ssnit\t"<<"taxable income\t"<<"income tax\t"<< "transport\t rent\t"<<" total deduction\t"<< "take home \n";


const int names=14;
const int attributes=6;
string employee[names][attributes]={
{"AB/1001","KOFI","MALE","SOFTWARE","31-AUG-10","SENIOR" },
{"AB/1002","AMA","FEMALE","HARDWARE","31-AUG-10","SENIOR" },
{"AB/1003","AKOS","FEMALE","HARDWARE","31-AUG-10","JUNIOR" },
{"AB/1004","JAMES","MALE","HARDWARE","31-AUG-10","JUNIOR" },
{"AB/1005","JONES","MALE","PRODUCTION","31-AUG-10","SENIOR" },
{"AB/1006","BEAUTY","FEMALE","PRODUCTION","31-AUG-10","JUNIOR" },
{"AB/1007","KWESI","MALE","HARDWARE","31-JUL-12","JUNIOR" },
{"AB/1008","AFI","FEMALE","SOFTWARE","31-JUL-12","JUNIOR" },
{"AB/1009","ROGER","MALE","SOFTWARE","31-JUL-12","SENIOR" },
{"AB/1010","WISDOM","MALE","HARDWARE","31-JUL-12","JUNIOR" },
{"AB/1011","HOPE","MALE","PRODUCTION","31-JUL-12","JUNIOR" },
{"AB/1012","MAGGI","FEMALE","PRODUCTION","31-JUL-12","JUNIOR" },
{"AB/1013","FRED","MALE","HARDWARE","31-JUL-12","SENIOR" },
{"AB/1014","PAT","FEMALE","HARDWARE","31-JUL-12","JUNIOR" },



}
;//end of array initialisation for employees


double wages[11];

for(int a=0;a<=13;a++){

if(employee[a][5]=="SENIOR"){
wages[0]=120000;//annual salary
wages[1]=0.15;//income tax rate
wages[2]=600;//transport allowance
wages[3]=300; //rent

}//end of if
else if(employee[a][5]=="JUNIOR"){
wages[0]=40000;//annual salary
wages[1]=0.10;//income tax rate
wages[2]=300;//transport allowance
wages[3]=150; //rent

}//end of else
wages[4]=wages[0]/12;//monthly basic
wages[5]=wages[4]*0.05;//ssnit employee
wages[6]=wages[4]*0.125;//ssnit employer
wages[7]=wages[4]-wages[6];//taxable income
wages[8]=wages[7]*wages[1];//income tax
wages[9]=wages[5]+wages[8];//total deduction
wages[10]=wages[2]+wages[3];//total allowance
wages[11]=wages[4]+wages[10]-wages[9];//take home
//ready for printing.
}//end of major for loop
for (int m=0;m<=13;m++){
for(int n=0;n<=5;n++){
cout<<employee[m][n]<<"\t";


}
for(int b=0;b<=10;b++){
cout <<wages[b]<<"\t";
}
cout<<endl;

}//end of printing for loop


return 0;
}
.
Last edited on
Hello yawpius,

I can assure you the the else or (else if) does not override the if statement, the for loop does that for you. The employee array is 2D yet the wage array is 1D so the for loop overwrites the the first 13 iterations leaving you with the wage information only for the 14 employee.

Next you defined double wages[11]; which can only hold information for one person. But, when your code says: wages[11]=wages[4]+wages[10]-wages[9];//take home you are trying to put a 12th element into an array of size 11. No one knows what you might be changing. double wages[11]; should be defined as double wages[14][12]; to work properly.

You will need to change your loops for print to accommodate the 2D wages array.

Hope that helps,

Andy

P.S. Forgot to mention the if else if can just be an if else.
Last edited on
Topic archived. No new replies allowed.