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

标签 c++ loops

谁能发现我在这个循环中的错误?阅读回复后,我似乎陷入了 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/

相关文章:

function - 如何定义动态嵌套循环python函数

c++ - 一个奇怪的内存泄漏问题

python - 遍历元组列表

javascript - 基于 name 属性而不是 id 使用 jQuery 循环元素

c++ - 虚拟析构函数的默认覆盖

c# - 在循环中捕获异步错误

java - 从方法内的决策语句调用时 println 不会发生(在 Java 中)

c++ - 从在 "user"帐户下运行的服务调用 OpenWindowStation

c++ - 这个纹理是如何传递给片段着色器的?

c++ - 派生公共(public)静态 std::list