Post 使用 char 值时测试循环不工作

Post test loop not working when using a char value

我一直试图让这段代码工作,但是当我进行 Char 值 post 测试时我遇到了麻烦,post 测试应该只在一个字符不是"Y" 或 "N"("y" 或 "n")由用户输入,但无论我输入什么,它都会一直循环也想知道是否有可能相同如果我在下面的代码的保险计划部分中循环执行相同的操作,可能会出现问题吗?任何帮助表示赞赏。

#include <iostream>
#include <iomanip>
using namespace std;
int main() {

    //variables
    double hours = 0;
    double rate = 0;
    double gross = 0;
    double state = 0;
    double fed = 0;
    double local = 0;
    double FICA = 0;
    double netpay = 0;
    double ins = 0;
    double otHrs = 0.0;
    double otPay = 0.0;
    double dep = 0;
    char sol;
    char fam;

    //Proccess & check
    do {                                                                    // Hours Post-Check
        cout << "Enter your hours worked: ";
        cin >> hours;
            if (hours < 0) {
                cout << "Enter a number of hours greater than 0" << endl;
            }
    } while (hours < 0);

    do {                                                                    //Pay Post-Check
        cout << "Enter your hourly pay: ";
        cin >> rate;
            if (rate < 11 || rate > 75) {
                cout << "Enter a pay rate that is greater than 11 or less than 75" << endl;
            }
    } while (rate < 11 || rate > 75);

    do {                                                                    //Dependent Post-Check
        cout << "Enter the number of dependents: ";
        cin >> dep;
            if (dep < 0) {
                cout << "Enter a number of dependents greater than 0" << endl;
            }
    } while (dep < 0);

    do {                                                                    //Healthcare Post-Check
        cout << "Healthcare? (Y/N): ";
        cin >> sol;
        if (sol != 'Y' || 'N') {                                            //Having trouble with this line of code is 
            cout << "Enter Y or N" << endl;                                 //This because I am using a Char versus a double/int?
            }                                                               //Im trying to have it loop only if sol does not equal Y or N (y or n)
    } while (sol != 'Y'|| 'N');                                             //It just continues the loop infinitely
                                                                            //will this problem be the same if i try to do this below in the insurance plan?
    //Calculating OverTime
    if (hours > 40) {
        otHrs = hours - 40;
        otPay = (rate * 1.5) * otHrs;
        gross = otPay + 40 * rate;
    }
    else {
        gross = (hours * rate);
    }


    //Dependents
    if (dep <= 2) {
        fed = 0.18 * gross;
    }
    else if (dep <= 3 && dep <= 6) {
        fed = 0.15 * gross;
    }
    else if (dep > 6) {
        fed = 0.13 * gross;
    }
    //Deductions
    state = .037 * gross;
    local = .01 * gross;
    FICA = .062 * gross;
    netpay = gross - (state + fed + local + FICA);

    //Insurance Y(y) or N(n)
    if (sol == 'Y' || sol == 'y') {                         
        cout << "Individual or Family plan (I/F): ";
        cin >> fam;

        //insurance plan
        if (fam == 'I' || fam == 'i') {
            ins = 35.00;
            netpay -= ins;
        }
        else if (fam == 'F' || fam == 'f') {
            ins = 60.00;
            netpay -= ins;
        }
    }






    cout << fixed;
    cout << setprecision(2) << endl;
    //display
    cout << "Gross pay:     $" << setw(11) << gross << endl;
    cout << "Federal tax:  " << setw(18) << fed << endl;
    cout << "State tax:  " << setw(20) << state << endl;
    cout << "local tax:  " << setw(20) << local << endl;
    cout << "FICA tax:  " << setw(21) << FICA << endl;
    cout << "Health insurance:   " << setw(12) << ins << endl;
    cout << "--------------------------------" << endl;
    cout << "Net Pay:       $" << setw(11) << netpay << endl;

    cout << endl << endl;
    return 0;
}

问题出在您的 while 条件(还有 while 循环上方几行的 if 条件):

while (sol != 'Y'|| 'N')

使用双管道时 'or' ||,每一边都是独立评估的。左侧(假设 sol == 'Y')的计算结果为 False。但右侧 'N' 将始终为真,因为字符 'N' 不是 0(假)。修复:

while (sol != 'Y' && sol != 'N')

祝你好运!