gpt4 book ai didi

c++ - 如果语句 "Your Code Will Never Be Executed"

转载 作者:太空宇宙 更新时间:2023-11-04 14:41:04 25 4
gpt4 key购买 nike

底部的 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;
}

}

最佳答案

你应该改变if条件

(pets == 'Y' || 'y')

(pets == 'Y' || pets == 'y')

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

对于所有其他 if 条件都是一样的。

关于c++ - 如果语句 "Your Code Will Never Be Executed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36159380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com