简体   繁体   中英

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 ,
  • FICA withholdings .
    I am getting the correct output, however my professors grader keeps telling giving me the error statement:

Format Ok. Build Ok. Link Ok. Run Timed out. Runtime 9020 milliseconds. 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)

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 . For example, if you input gross pay " 100,50 " you input comma but you need to input point: " 100.50 ". If you input comma than gross pay is set to 100 and " ,50 " left in the std::cin stream. " ,50 " can not be converted to double in the next input ( cin >> federalTax; ). As result, federalTax is not changed (if you use c++03 ) or set to 0 (if you use 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. 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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