c ++赋值在if语句

时间:2016-09-22 03:54:29

标签: c++ arrays

嗨我正在使用两个数组为指定员工创建工资单。一个用于保存员工详细信息,一个用于所需的计算。具有高级职位的员工向初级会员支付不同的税和租金。我试图在for循环中使用条件来使用条件来分配不同的租金和税值,但是初级(后者如果)覆盖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;
}

2 个答案:

答案 0 :(得分:1)

问题是您只有一个数组wages来存储所有员工的信息。因此,当您打印wages时,您只打印与上一名员工相对应的工资信息。

您可以使用以下方法之一解决此问题。

  1. 计算后立即在员工循环中打印工资。
  2. 使用二维数组存储工资,然后像现在一样在最后打印它们。
  3. 解决方案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
    

答案 1 :(得分: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);

为员工做同样的事情