繁体   English   中英

需要有关无限循环的帮助

[英]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
    我得到了正确的 output,但是我的教授分级员一直告诉我错误声明:

格式确定。 构建确定。 链接好的。 运行超时。 运行时间 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;
}

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

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 :";
}

不要在条件中使用 while .. 如果您从 cin 存储了值,请不要尝试在 while 循环中检查其有效性.. 仅使用 if 语句.. 导致 cin 在等待输入时“停止”程序...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM