需要有关无限循环的帮助
Need help regarding an infinite loop
我目前正在处理一个家庭作业问题,要求我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 :";
}
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 超出 int 的值范围后的无限循环
- 为什么这段代码会导致无限循环?
- 运行无限循环的最小二叉树问题
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 连接线程时的无限循环
- 为什么这种方法会进入无限循环?
- 由于QTextStream的无限循环
- 需要有关无限循环的帮助
- 我需要帮助找到我拥有的无限循环
- 在C++链接列表中帮助.show记录功能启动无限循环