需要有关无限循环的帮助

Need help regarding an infinite loop

本文关键字:帮助 无限循环      更新时间:2023-10-16

我目前正在处理一个家庭作业问题,要求我
Write a program that displays a weekly payroll report.
程序中的循环应该询问用户:

  • employee number
  • gross pay
  • state tax
  • federal tax
  • FICA withholdings.
    我得到了正确的输出,但是我的教授评分员一直告诉我错误陈述:
格式正常。构建正常。

链接正常。运行超时。运行时 9020 毫秒。您的程序正在等待输入或处于无限循环中。

我在找到这个循环时遇到了一些麻烦,如果有人能向我指出这一点,我将不胜感激。

我尝试更改主while(employeeNumber !=0)循环以包含重复if (totalWithholdings > grossPay)do/while语句

int main()
{
int employeeNumber;
double grossPay = 0.0,
grossPayTotal = 0.0,
stateTax = 0.0,
stateTaxTotal = 0.0,
federalTax = 0.0,
federalTaxTotal = 0.0,
ficaWithholdings = 0.0,
ficaWithholdingsTotal = 0.0,
netPay = 0.0,
netPayTotal = 0.0,
totalWithHoldings = 0.0;
cout << "Enter the following information:n" << endl;
cout << "Employee Number(0 to quit) :n";
cin >> employeeNumber;
while (employeeNumber < 0)
{
cout << "Employee number may not be less than zero.n";
cout << "Re-enter employee Number (0 to quit): ";
cin >> employeeNumber;
}

while (employeeNumber != 0)
{


cout << "Gross pay :";
cin >> grossPay;
while (grossPay < 0)
{
cout << "Gross pay may not be less than zero.n";
cout << "Re-enter Gross pay: ";
cin >> grossPay;
}
cout << "Federal Withholding :";
cin >> federalTax;
while (federalTax < 0)
{
cout << "Federal witholding may not be less than zero.n";
cout << "Re-enter Federal Withholding: ";
cin >> federalTax;
}
cout << "nState Withholding :";
cin >> stateTax;
while (stateTax < 0)
{
cout << "nState witholding may not be less than zero.n";
cout << "nRe-enter State Withholding: ";
cin >> stateTax;
}
cout << "FICA Withholding : ";
cin >> ficaWithholdings;
while (ficaWithholdings < 0)
{
cout << "FICA witholding may not be less than zero.n";
cout << "Re-enter FICA Withholding: ";
cin >> ficaWithholdings;
}
totalWithHoldings = (federalTax + stateTax + ficaWithholdings);
if (totalWithHoldings > grossPay)
{
cout << "nERROR: Withholdings cannot exceed gross pay.n"
<< "nPlease re-enter the data for this employee.n";
cin >> employeeNumber;
}
else
{
cout << "Processing the Next employee:n"
<< "Employee Number(0 to quit) :n";
cin >> employeeNumber;
}
grossPayTotal = grossPayTotal + grossPay;
federalTaxTotal = federalTaxTotal + federalTax;
stateTaxTotal = stateTaxTotal + stateTax;
ficaWithholdingsTotal = ficaWithholdingsTotal + ficaWithholdings;
netPay = grossPay - totalWithHoldings;
netPayTotal = netPayTotal + netPay;
}

cout << "Total Gross Pay    : $" << setw(4) << setprecision(2)
<< fixed << grossPayTotal << endl;
cout << "Total Federal Tax  : $" << setw(4) << setprecision(2)
<< fixed << federalTaxTotal << endl;
cout << "Total State Tax    : $" << setw(4) << setprecision(2)
<< fixed << stateTaxTotal << endl;
cout << "Total FICA         : $" << setw(4) << setprecision(2)
<< fixed << ficaWithholdingsTotal << endl;
cout << "Total Net Pay      : $" << setw(4) << setprecision(2)
<< fixed << netPayTotal << endl;

return 0;
}

当您键入无法解释为双精度的符号时,会出现此问题。例如,如果您输入总工资"100,50",则输入逗号,但您需要输入点:">100.50"。如果您输入逗号,则总工资设置为 100,并在 std::cin 流中留下">,50"。">,50" 不能在下一个输入中转换为双精度 (cin>> federalTax;(。因此,FederalTax 不会更改(如果使用 c++03(或设置为 0(如果使用c++11(。详细信息在文章中。因此,(对于 c++03(您输入数据的每个位置 (std::cin( 都不会更改相应的变量,",50"仍在输入流中(因此,无需等待下一个用户输入(,并且在每个循环结束时 employeeNumber 也没有更改,并且while 的条件 (employeeNumber != 0(当然是正确的,其中形成了一个无限循环。您可以使用下一个示例检查每个输入:

cout << "Gross pay :";
while (!(cin >> grossPay)) {
std::cin.clear(); // clear all error state flags
std::string inputLine;
std::getline(std::cin, inputLine); // "remove" invalid data from std::cin
cout << "Please enter a valid gross pay" << endl;
cout << "Gross pay :";
}