gpt4 book ai didi

c++ - 重叠计算

转载 作者:行者123 更新时间:2023-11-28 03:02:31 26 4
gpt4 key购买 nike

由于某种原因,第二次迭代此循环时,cout 语句重叠。换句话说,在第一次 cout 之后,程序不会等待输入。我该如何解决这个问题?

此外,对于现实生活中的税收,nPay 功能是否正常工作?有人告诉我,税收应该乘以毛额,逐个计算,然后相加。但是,我的方法同样有效,特别是因为它们同时发生。

double calcGrossPay (double payRate, double hours);
double nPay (double& fedTx, double& localTx, double& stateTx, double& ssTx, double& netPay, double fPay);
void displayAll (double fPay, double netPay, string name);

double fedTx = 14, stateTx = 6, localTx = 3.5, ssTx = 4.75;

int main()
{

while (!cin.eof())
{
string name;

//cin.ignore();
cout <<"Please enter your working name: ";
getline (cin, name);
!cin.eof();

double payRate, hours;

cout <<"Enter your pay rate and hours worked, respectively."<< endl;
cin >> payRate >> hours;
!cin.eof();

double fPay = calcGrossPay (payRate, hours);

double netPay = 0;
netPay = nPay (fedTx, localTx, stateTx, ssTx, netPay, fPay);
displayAll (fPay, netPay, name);

system("pause");
}
}


double calcGrossPay (double payRate, double hours)
{
double extraT, fPay;
if (hours > 40)
{
extraT = (hours - 40) * (1.5 * payRate);
fPay = extraT + (40 * payRate);
}
else
fPay = payRate * hours;

return fPay;
}

double nPay (double& fedTx, double& localTx, double& stateTx, double& ssTx, double& netPay, double fPay)
{
double totalTx = fedTx + localTx + stateTx + ssTx;
netPay = fPay * (1 - (totalTx / 100));
return netPay;
}

void displayAll (double fPay, double netPay, string name)
{
cout <<"Below is "<< name << "'s salary information" << endl;

cout << fixed << showpoint << setprecision(2) <<"\nYour calculated gross pay is $"
<< fPay << ", and your net pay is $" << netPay << endl;
}

最佳答案

getline 之后,新的一行仍在流中,因此您必须忽略它:

getline(cin, name);
cin.ignore();

此外,在检查流之前执行提取而不是 while (!cin.eof()):

while (getline(cin, name))
{
cin.ignore();
// ...
}

这是更新后的代码。我希望它对你有用:

int main()
{
for (std::string name; (cout << "Please enter your working name: ") &&
getline(cin >> std::ws, name);)
{
if (cin.eof())
break;

double payRate, hours;

cout << "\nEnter your pay rate and hours worked, respectively." << endl;

if (!(cin >> payRate >> hours))
break;

double fPay = calcGrossPay(payRate, hours);

double netPay = nPay(fedTx, localTx, stateTx, ssTx, netPay, fPay);

displayAll(fPay, netPay, name);
cin.get();
}
}

关于c++ - 重叠计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20389017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com