小编Ada*_*mer的帖子

做... while循环不破坏c ++

目前我遇到了这个问题... while循环.

do { 
 // program code here
    cout << "Would you like to run the program again?(yes/no)";
    bool exit = false;
    string strexit;
    do {
        getline(cin, strexit);
        if (strexit == "no") {
            exit = false;
            break;
        }
        else if (strexit == "yes") {
            exit = true;
        }
        else {
            cout << "Enter yes to rerun the program, and no to exit.\n";
        };
    } while (!exit);
    system("cls");
} while (exit);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在网上研究过,如何摆脱do ... while循环,当条件为真时,它会再循环回来,但如果它是假的,它会退出.因此,如果您查看代码,如果用户键入no,则设置exit = false,这将从更大的do while循环中取出,其中break将其从当前do while循环中取出.如果用户输入yes,则将exit更改为true,这会将其从当前的do …

c++ break do-while

-3
推荐指数
1
解决办法
905
查看次数

标签 统计

break ×1

c++ ×1

do-while ×1