如果声明"您的代码永远不会被执行"

0 c++

底部的if else不起作用; 它说宠物并不存在.

void checkIn ()
{

    int roomNum = 0;
    int party = 0;
    char pets;
    char smoke;

    cout << "----------------------------" << endl;
    cout << "Welcome to Shrek's Swamp Inn" << endl << endl;

    cout << "How many people are In your party? " << endl;
    cin >> party;

    cout << "Do you have pets? (Y/N)" << endl;
    cin >> pets;

    switch (pets)
    {
        case 'Y' : case 'y':
            cout << "GTLO" << endl;
            break;
        case 'N' : case 'n':
            cout << "cool cool" << endl;
            break;
        default :
            cout << "User error" << endl;
    }

    cout << "Do you want a smoke room? (Y/N) " << endl;
    cin >> smoke;

    switch (smoke)
    {
        case 'Y' : case 'y':
            cout << "GTLO" << endl;
            break;
        case 'N' : case 'n':
            cout << "cool cool" << endl;
            break;
        default :
            cout << "User error" << endl;
    }

    if((pets == 'Y' || 'y') && (smoke == 'Y' || 'y')){
        roomNum = rand() % 4 + 1;
        cout << "Your room is " << roomNum << "S" << endl;
    }
    else if((pets == 'Y' || 'y') && (smoke == 'n' || 'N')){
        roomNum = rand() % 8 + 5;

        cout << "Your room is " << roomNum << "P" << endl;
    }
    else if((pets == 'n' || 'N') && (smoke == 'n' || 'N'));{
        roomNum = rand() % 15 + 9;

        cout << "Your room is " << roomNum << "R" << endl;
    }

}
Run Code Online (Sandbox Code Playgroud)

son*_*yao 8

你应该改变if条件

(pets == 'Y' || 'y')
Run Code Online (Sandbox Code Playgroud)

(pets == 'Y' || pets == 'y')
Run Code Online (Sandbox Code Playgroud)

否则,'y'(作为非零值)将始终被评估true为if条件,然后(pets == 'Y' || 'y')将始终true也是如此.

对于所有其他条件,它都是一样的.