视觉我希望一个函数在另一个函数C++中进行计算

visual I want a function to make a calculation in another function C++

本文关键字:函数 C++ 另一个 计算 视觉 一个 我希望      更新时间:2023-10-16

我尝试在C++中创建一个函数,为变量赋值并保留它,但我想用void 函数制作。 这是主要功能;

int main(){
//declar variables
double sales=0.0,commission=0.0,totalCom=0.0,totalSales=0.0,totalEmployee=0.0;
//call soldEmployee function
sales = soldEmployee(sales);    
// if use enter sales negative the programe end
while(sales > 0){
//call calc commission
commisionCalc(sales,commission);
//call display commission
displayCom(commission);
//call display total (commission + sale)
disTotal(sales,commission,totalEmployee);
// ask user to put input again
sales = soldEmployee(sales);
}
total(totalSales,commission);
return 0;
} //end of main function

这就是功能;

double soldEmployee(double &s){//function to get the user input
cout << "Next Sales: "; cin >> s;   
return s;   
}
void commisionCalc(double s, double &com){//function to calc commission
com = s * 0.10;
}
void displayCom(double c){;// function to display commission
cout <<fixed << setprecision(2) << "Commision= " << c << endl;
}
void disTotal(double s,double com,double &total){// function to get commission + sales
total = s + com;
cout << "Salary + Com: " << total << endl;
}
void total(double t, double &total){// function to get the total of all employee commision and store it in total sales
t += total;
cout << "Total Salary: " << total << endl;
}

因此,在函数总数中,我想让它分配我进入totalSales的佣金,我知道变量的名称似乎令人困惑,但是,这是因为我在程序中进行了一些修改。 谁能帮我解释一下如何制作,因为我在制作许多功能时卡住了。 如果你问有一种简单的方法可以让我想要这种方式,因为这是一项任务,学习如何制作功能并习惯它不是工作。

我不确定我是否理解您的问题,但是如果您需要在total函数中更改 main 中的变量totalSales,以便可以从 main 访问其修改后的值,您应该执行以下操作:

// note below the "double &t" which is now passed by reference.
void total(double &t, double total){
t += total;
cout << "Total Salary: " << total << endl;
}