2016-09-22 63 views
-2

您好我正在爲使用兩個數組的指定員工創建工資單。一個用於保存員工詳細信息,另一個用於所需的計算。具有高級職位的員工向初級會員支付不同的稅款和租金。我試圖在for循環中使用條件來使用條件賦值不同的租金和稅值,但是初級(後者如果)忽略了if並且條件無法正常工作。 這是我的代碼C++作業不在if語句中工作

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

using namespace std; 
int main() 
{ 

    cout<<"\t\t\t\t business application developers association \n"; 


cout<<" \t\t\t\t\t\t payroll for month ending\n \t\t\t\t\t\t\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; 
} 
+0

您面臨的問題究竟是什麼? – user3286661

+0

我如果條件不起作用。我想通過使用if條件來計算老年人和青少年的不同稅率和租金津貼。但它給我的只有後輩 – pglinnaeus

回答

1

的問題是,你只有一個陣列,wages,存儲所有員工的信息。因此,當您打印wages時,您只打印對應於最後一名員工的工資信息。

您可以使用以下方法之一來解決此問題。

  1. 在您計算他們後,在員工循環中打印工資。
  2. 使用二維數組存儲工資,然後像現在一樣在最後打印它們。

解決方案1 ​​

計算工資和打印在一個循環。

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. 

    for(int n=0;n<=5;n++){ 
    cout<<employee[a][n]<<"\t"; 
    } 
    for(int b=0;b<=10;b++){ 
    cout <<wages[b]<<"\t"; 
    } 
    cout<<endl; 
}//end of major for loop 

解在第一環路2

計算工資和在第二循環中進行打印。

// Need a 2D array for the wages. 
double wages[13][11]; 

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

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

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

    }//end of else 
    wages[a][4]=wages[a][0]/12;//monthly basic 
    wages[a][5]=wages[a][4]*0.05;//ssnit employee 
    wages[a][6]=wages[a][4]*0.125;//ssnit employer 
    wages[a][7]=wages[a][4]-wages[a][6];//taxable income 
    wages[a][8]=wages[a][7]*wages[a][1];//income tax 
    wages[a][9]=wages[a][5]+wages[a][8];//total deduction 
    wages[a][10]=wages[a][2]+wages[a][3];//total allowance 
    wages[a][11]=wages[a][4]+wages[a][10]-wages[a][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[m][b]<<"\t"; 
    } 
    cout<<endl; 

}//end of printing for loop 
+0

你能請詳細說明嗎? – pglinnaeus

+0

@pglinnaeus,查看更新的答案。 –

+0

加上這是一個可怕的方式來存儲數據工資[0] =支付,工資[1] =稅,...你應該做一個結構和存儲在向量 – pm100

0

您的數據應該這樣表示

struct Wages 
{ 
    double Salary; 
    double TaxRate; 
    double TransportAllowance; 
    // etc 
} 

然後有

std::vector<Wages> EmployeeWages(); 

要爲特定的人

Wages w = new Wages(); 
if(employee[a][5]=="SENIOR"){ 
    w.Salary=120000;//annual salary 
    w.Tax=0.15;//income tax rate 
    w.TransportAllowance=600;//transport allowance 
    } 
    // etc 
EmployeeWages.push_back(w); 

創建的工資和做同樣的員工