C++ Can';我不计算最后的总数
我很难得到这个程序的最终总数,我有C++ Can';我不计算最后的总数,c++,C++,我很难得到这个程序的最终总数,我有total+=amount在函数中,但它不起作用,我已经设置了所有变量,但我不太确定在哪里可以放置一行代码来计算总数 #include <iostream> #include<string> #include<iomanip> using namespace std; float calculateCharges(float hours, float &total); int main() { int MA
total+=amount代码>在函数中,但它不起作用,我已经设置了所有变量,但我不太确定在哪里可以放置一行代码来计算总数
#include <iostream>
#include<string>
#include<iomanip>
using namespace std;
float calculateCharges(float hours, float &total);
int main()
{
int MAX=3;
float charge[MAX];
float hours[MAX];
float total=0;
int i;
int columns[]={10,9,10};
for(i=0; i<MAX; i++)
{
cout << "Enter the amount of hours for customer " <<i+1<<": ";
cin >> hours[i];
charge[i] = calculateCharges(hours[i], total);
}
string border;
border.assign(columns[0]+columns[1]+columns[2], '-');
cout<<border<<endl
<<left<<setw(columns[0])<<"customer"
<<setw(columns[1])<<"hours"
<<setw(columns[2])<<"charge"
<<endl;
for(i=0; i<MAX; i++)
cout<<left<<setw(columns[0])<<i+1
<<right<<setw(2)<<fixed<<setprecision(2)<<hours[i]
<<setw(columns[2])<<charge[i]<<endl;
cout<<border<<endl;
cout<<right<<setw(columns[0]+columns[1])<<"total: "<<left<<" $"<<total<<endl;
return 0;
}
float calculateCharges(float hours, float &total)
{
float amount = 20 + (5*((int)hours-3));
if((int)hours<=3)
return 20;
else return amount;
}
#包括
#包括
#包括
使用名称空间std;
浮动计算费用(浮动小时数、浮动和总计);
int main()
{
int MAX=3;
浮动电荷[最大值];
浮动小时[最大];
浮动总数=0;
int i;
int列[]={10,9,10};
对于(i=0;i操作+=“发生”在返回之后..因此它不会发生
我想你的意思是:
float calculateCharges(float hours, float &total)
{
float addToTotal = 20;
float amount = 20 + (5*((int)hours-3));
if((int)hours>3)
addToTotal= amount;
total+=addToTotal;
return addToTotal;
}
是否正确:assign(列[0]+列[1]+列[2],“-”);
?这将在传递到assign
函数之前添加列的前3个值。