有人会帮助我理解为什么这个循环不断重复?

时间:2015-04-14 06:31:41

标签: c++ loops

我为类编写了这段代码,除了完整的程序循环外,一切都运行正常,询问用户是否要重复该程序。它似乎无法识别输入字符以结束循环。任何帮助表示赞赏。

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#define REGULAR_HOURS_LIMIT 40.0    // hours at which overtime begins
#define OVERTIME_HOURS_LIMIT 20.0   // Maximum overtime hours
#define OVERTIME_RATE 1.5           // overtime rate
#define TAX_RATE 0.30               // tax rate (30%)
#define PARKING_RATE 10.00          // parking deduction
#define MAXIMUM_PAY_RATE 99.99      // Maximum employee pay

int main()
{
    string  first_name,     // employee's first name, input by user
            last_name,      // employee's last name, input by user
            full_name;      // employee's concatenated full name

    double  hours,          // number of hours worked, input by user
            regular_hours,  // number of regular hours
            overtime_hours, // number of overtime hours
            hourly_rate,    // hourly pay rate
            gross_pay,      // employee's gross pay
            tax,            // employee's tax amount
            deductions,     // monthly deductions
            net_pay;        // employee's net pay

    char    yesno;          // Prompt for new input 

    do
    {
// Input section

    cout << "Enter employee's first name: ";
    cin >> first_name;

    cout << "Enter employee's last name: ";
    cin >> last_name;

    cout << "Enter number of hours worked: ";
    cin >> hours;
        while (hours < 0 || hours > (REGULAR_HOURS_LIMIT + OVERTIME_HOURS_LIMIT))
        {
            cout << "Hours must be between 0 and 60" << endl;
            cout << "Enter number of hours worked: ";
            cin >> hours;
        }

    cout << "Enter hourly pay rate: $";
    cin >> hourly_rate;
        while (hourly_rate < 0 || hourly_rate > MAXIMUM_PAY_RATE)
        {
            cout << "Pay rate must be between 0.00 and 99.99" << endl;
            cout << "Enter hourly pay rate: $";
            cin >> hourly_rate;
        }

// Processing section

    full_name = last_name + ", " + first_name;

    if (hours <= REGULAR_HOURS_LIMIT) 
    {
        regular_hours = hours;
        overtime_hours = 0.0;
    }

    else 
    {
        regular_hours = REGULAR_HOURS_LIMIT;
        overtime_hours = hours - REGULAR_HOURS_LIMIT;
    }

    gross_pay = (regular_hours * hourly_rate) + (overtime_hours *

    hourly_rate * OVERTIME_RATE);

    tax = gross_pay * TAX_RATE;

    deductions = PARKING_RATE;

    net_pay = gross_pay - tax - deductions;

// Output section

cout << endl;
cout << "12345678901234567890##21.00##21.00##321.00##4321.00##321.00"
<< "##321.00##4321.00" << endl;
cout << " Reg. Ovt. Hourly Gross "
<< " Net " << endl;
cout << "Name Hours Hours Rate Pay Taxes "
<< " Deduct Pay " << endl;
cout << "==================== ===== ===== ====== ======= ======"
<< " ====== =======" << endl;
cout << left << setw(20) << full_name << " ";
cout << right << fixed << setprecision(2);
cout << setw(5) << regular_hours << " ";
cout << setw(5) << overtime_hours << " ";
cout << setw(6) << hourly_rate << " ";
cout << setw(7) << gross_pay << " ";
cout << setw(6) << tax << " ";
cout << setw(6) << deductions << " ";
cout << setw(7) << net_pay << endl << endl;


cout << "Process another employee (Y/N)?";
cin >> yesno;
    if (yesno == 'y' || 'Y' || 'n' || 'N')
    {
        cout << endl;
    }
    else
    {
        do
        {
            cout << "Please type 'Y' for yes or 'N' for no";
            cin >> yesno;
        }
        while (yesno != 'y' || 'Y' || 'n' || 'N');
    }
    }
    while (yesno == 'y' || 'Y');
}

3 个答案:

答案 0 :(得分:3)

更改行

while (yesno == 'y' || 'Y');

while ((yesno == 'y') || (yesno == 'Y'));

在第一种情况下&#39; Y&#39;永远是真的,你有无限循环 行

也是如此
if (yesno == 'y' || 'Y' || 'n' || 'N')
...
while (yesno != 'y' || 'Y' || 'n' || 'N');

在这一行&#39; y&#39;,&#39; Y&#39;和其他人的隐瞒转变为布尔 根据规则

  

将零值,空指针值或空成员指针值转换为false;任何其他值都转换为true。

答案 1 :(得分:0)

在这一行:

 while (yesno != 'y' || 'Y' || 'n' || 'N');

你必须写yesno!='y'&amp;&amp; yesno!='Y'&amp;&amp; yesno!='n'&amp;&amp; yesno!='N'

答案 2 :(得分:0)

while(hourly_rate&lt; 0 || hourly_rate&gt; MAXIMUM_PAY_RATE) 为什么你循环这个?它始终是真/假,使用 if(hourly_rate&lt; 0 || hourly_rate&gt; MAXIMUM_PAY_RATE)来检查条件。  whille用于循环