在C ++中的函数之间传递值

时间:2014-09-21 06:40:54

标签: c++

我正在为只有6名推销员的汽车公司制作一个程序。该程序将要求用户输入每个销售人员的姓名和他的月薪,输入他一个月内售出的汽车数量,以及每辆汽车的品牌和价格。该程序将显示每个销售员的月收入(工资+佣金),销售人员的平均月收入,收入最高的推销员的名称(和收入),销售员的名称(和收入)与最低收入,每个品牌销售的汽车数量和最受欢迎 品牌。

我坚持从Salesman1()和Salesman2()传递totalCommission的值。另一个问题是,我不知道我是否可以将字符串CIN转换为名为SalesmanName的数组。这是我到目前为止所做的。

int main()
{       
int cycle=1;
int SalesMan1(),SalesMan2();


do
{
    switch(cycle)
    {
        case 1: SalesMan1(); cycle++; break;
        case 2: SalesMan2(); cycle++; break;
        /*case 3: SalesMan3(); cycle++; break;
        case 4: SalesMan4(); cycle++; break;
        case 5: SalesMan5(); cycle++; break;
        case 6: SalesMan6(); cycle++; break;*/
        default: cout<<"Printing out reports"<<endl;
                 system("\npause");
    }
}while(cycle<=6);  

return 0;                                                                                              

}

int SalesMan1()
{
  char NameOne[40];
  int numCar=1,carSold,carType,audiSold=0,bmwSold=0,mbenzSold=0;
  double salary,carPrize,commission,totalCommission,allMonthlyIncome;

  cout<<"Name of 1st Salesman:"<<endl;
  cin.getline(NameOne,39);

  cout<<"\nMonthly Salary: RM";
  cin>>salary;

  cout<<"\nNumber of car sold in a month: ";
  cin>>carSold;
  system("CLS");

  do{
      cout<<"\nEnter type of car sold as below: "<<endl;
      cout<<"\n1-Audi"<<endl;
      cout<<"2-BMW"<<endl;
      cout<<"3-Mercedes Benz"<<endl;
      cout<<"\nCar Type:";
      cin>>carType;

      switch(carType)
      {
        case 1:
            cout<<"\nEnter the price sold: RM";
            cin>>carPrize;
            commission=0.02*carPrize;
            totalCommission+=commission;
            audiSold++;
            system("CLS");
            break;

        case 2:
            cout<<"\nEnter the price sold: RM";
            cin>>carPrize;
            commission=0.02*carPrize;
            totalCommission+=commission;
            bmwSold++;
            system("CLS");
            break;

        case 3:
            cout<<"\nEnter the price sold: RM";
            cin>>carPrize;
            commission=0.035*carPrize;
            totalCommission+=commission;
            mbenzSold++;
            system("CLS");
            break;

        default :
            cout<<"\nNot available!"<<endl;
            system("pause");
            return 1;
      }
      numCar++;
  }while(numCar<=carSold);

  allMonthlyIncome=allMonthlyIncome+totalCommission+salary;

}

int SalesMan2()
{
  char NameTwo[40];
  int numCar=1,carSold,carType,audiSold=0,bmwSold=0,mbenzSold=0;
  double salary,carPrize,commission,totalCommission,allMonthlyIncome;

  cout<<"Name of 2nd Salesman"<<endl;
  cin.getline(NameTwo,39);

  cout<<"\nMonthly salary: RM";
  cin>>salary;

  cout<<"\nNumber of car sold in a month: ";
  cin>>carSold;
  system("CLS");

  do{
      cout<<"\nEnter type of car sold as below: "<<endl;
      cout<<"\n1-Audi"<<endl;
      cout<<"2-BMW"<<endl;
      cout<<"3-Mercedes Benz"<<endl;
      cout<<"\nCar Type:";
      cin>>carType;

      switch(carType)
      {
        case 1:
            cout<<"\nEnter the price sold: RM";
            cin>>carPrize;
            commission=0.02*carPrize;
            totalCommission+=commission;
            audiSold++;
            system("CLS");
            break;

        case 2:
            cout<<"\nEnter the price sold: RM";
            cin>>carPrize;
            commission=0.02*carPrize;
            totalCommission+=commission;
            bmwSold++;
            system("CLS");
            break;

        case 3:
            cout<<"\nEnter the price sold: RM";
            cin>>carPrize;
            commission=0.035*carPrize;
            totalCommission+=commission;
            mbenzSold++;
            system("CLS");
            break;

        default :
            cout<<"\nNot available!"<<endl;
            system("pause");
            return 1;
     }
     numCar++;
  }while(numCar<=carSold);

   allMonthlyIncome=allMonthlyIncome+totalCommission+salary;
}

2 个答案:

答案 0 :(得分:2)

SalesmanN()函数中,以:

结尾
return totalCommission;

main()中,执行:

int allCommissions = 0;

然后在循环中执行:

case 1: allCommissions += SalesMan1(); cycle++; break;

答案 1 :(得分:0)

您可以CIN到任何字符数组,但它不是名称的首选,因为CIN限制其在空格上的输入。 SalesmanName没有任何空格。

char arr[1000]; 
cin`>>arr;

如果输入“XYZ ABC”作为名称,则arr []将仅存储“XYZ”,而“ABC”将保留在输入流中。

对于这个问题,您可以创建一个Salesman类,它将封装每个Salesman所需的所有函数和数据。由于公司只有6名工人,你可以制作一个由6个Salesman Class对象组成的数组。因此,您可以将特定销售员的佣金称为SalesMan [i] .getCommission()。此外,课程将有助于以后扩展到更多的工人。