简体   繁体   English

需要有关无限循环的帮助

[英]Need help regarding an infinite loop

I am currently working on a homework problem that asks me to我目前正在处理一个要求我做的家庭作业问题
Write a program that displays a weekly payroll report.
A loop in the program should ask the user:程序中的循环应该询问用户:

  • employee number
  • gross pay
  • state tax
  • federal tax , federal tax
  • FICA withholdings . FICA withholdings
    I am getting the correct output, however my professors grader keeps telling giving me the error statement:我得到了正确的 output,但是我的教授分级员一直告诉我错误声明:

Format Ok.格式确定。 Build Ok.构建确定。 Link Ok.链接好的。 Run Timed out.运行超时。 Runtime 9020 milliseconds.运行时间 9020 毫秒。 Your program is waiting for input or in an endless loop.您的程序正在等待输入或处于无限循环中。

I am having some trouble finding this loop and would appreciate if anyone could point it out to me.我在找到这个循环时遇到了一些麻烦,如果有人能向我指出,我将不胜感激。

I have tried changing the main while(employeeNumber !=0) loop to include a do/while statement that repeats if (totalWithholdings > grossPay)我尝试更改主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;
}

The problem occurs when you type such symbols which can not be interpreted as double .当您键入无法解释为double的此类符号时,就会出现问题。 For example, if you input gross pay " 100,50 " you input comma but you need to input point: " 100.50 ".例如,如果您输入工资总额“ 100,50 ”,您输入逗号,但您需要输入点:“ 100.50 ”。 If you input comma than gross pay is set to 100 and " ,50 " left in the std::cin stream.如果您输入逗号,则将总工资设置为 100,并且在 std::cin stream 中留下“ ,50 ”。 " ,50 " can not be converted to double in the next input ( cin >> federalTax; ). " ,50 " 不能在下一个输入 ( cin >> FederalTax; ) 中转换为双精度。 As result, federalTax is not changed (if you use c++03 ) or set to 0 (if you use c++11 ).因此, FederalTax 不会更改(如果您使用c++03 )或设置为 0 (如果您使用c++11 )。 Details are in the article .详情在文章中 So, (for c++03) each place where you input data (std::cin) does not change corresponding variable, " ,50 " is still in input stream (so, no waiting of the next user input), and at the end of each loop employeeNumber also is not changed, and than condition of while (employeeNumber != 0) of course is true , where an endless loop is formed.因此,(对于 c++03)您输入数据的每个位置(std::cin)都不会更改相应的变量,“ ,50 ”仍在输入 stream 中(因此,无需等待下一个用户输入),并且在每个循环的结尾employeeNumber 也没有改变,而while (employeeNumber != 0)的条件当然是true ,形成了一个无限循环。 You can check each input using the next example:您可以使用下一个示例检查每个输入:

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

Don't use while for conditions.. if you have value stored from cin, dont try to check its validity in while loop.. use just if statement.. cause cin "stops" the program while waiting for input...不要在条件中使用 while .. 如果您从 cin 存储了值,请不要尝试在 while 循环中检查其有效性.. 仅使用 if 语句.. 导致 cin 在等待输入时“停止”程序...

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

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