gpt4 book ai didi

c++ - 卡在 while 循环中的 if 语句中

转载 作者:太空宇宙 更新时间:2023-11-04 15:27:27 24 4
gpt4 key购买 nike

谁能发现我在这个循环中的错误?阅读回复后,我似乎陷入了 If 语句。此外,在条件不为真的情况下插入 break 以退出循环似乎会引发错误:“Expected primary expression before else”

代码:

while (rowNum > (FC_Row))
{
cout << "That row is not located in our first class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
cin >> reply;
reply = toupper(reply);
while (reply != 'Y' && reply !='N')
{
cin.clear();
while(cin.get()!='\n');
cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
cout << "try again:" << endl;
cin >> reply;
reply = toupper(reply);
}
if (reply = 'Y')
ticketType = 'E';
break; // I want this to exit the while loop, I get a syntax error from this break.
else
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}

最佳答案

您需要大括号,并在 if 条件中将 = 替换为 ==:

if (reply == 'Y')
{
ticketType = 'E';
break;
}
else
{
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}

关于c++ - 卡在 while 循环中的 if 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5284961/

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