简体   繁体   English

如何在 C++ 中循环 while 循环

[英]How do I loop a while loop in C++

Im new to programing in a whole and I was wondering how I could loop my while loop.我是一个整体编程的新手,我想知道如何循环我的 while 循环。 I'm making a calculator and i've gotten to a part where I have the program ask whether or not the user wants to end the program, if the user answers "Yes" the program will end;我正在制作一个计算器,我已经到了让程序询问用户是否要结束程序的部分,如果用户回答“是”,程序将结束; however I have noticed that if the user answers "No" the program will just keep on working and not ask the question again.但是我注意到,如果用户回答“否”,程序将继续工作,不再问问题。 Is there a way where I can have it ask the question again?有没有办法让它再次问这个问题?

  while (response != "Yes" && response != "No") {
        cout << "Would you like to end the program? Yes or No" << endl;
        cin >> response;

        if (response == "Yes") {
            calculator_running = false;
        } else if (response == "No") {
            calculator_running = true;
        } else {
            cout << "Please choose a valid response" << endl;
        }
    }

Best practice is to split code to smaller pieces to keep concerns separated.最佳实践是将代码拆分为更小的部分,以保持关注点分开。

bool promptYesNo(const std::string& reason)
{
    std::cin.clear(); // clear any error flags on cin
    std::cout << reason << "\nType \"Yes\" or \"No\": ";
    std::string answear;
    while (std::cin >> answear) {
       if (answear == "Yes") return true;
       if (answear == "No") return false;
       std::cout << "Please select \"Yes\" or \"No\": ";
    }
    // here standard input has ended, so terminating application:
    std::exit(1);
}

while (!promptYesNo("Would you like to end the program?")) {
    ...
}

Note that std::cin.clear();注意std::cin.clear(); will protect you from invalid state of std::cin .将保护您免受std::cin的无效 state 的影响。 Most probably this is source of your problems.很可能这是您的问题的根源。 For example some part of program was reading int value, but you have provided a letters.例如,程序的某些部分正在读取int值,但您提供了一个字母。 This setts error flags on cin and any later reads will fail.这会在cin上设置错误标志,以后的任何读取都将失败。

You need to put the calculator_running to be checked in the while-part of the loopo, something like this:您需要将要检查的calculator_running放在循环的while 部分中,如下所示:

calculator_running = true;
while (calculator_running)
...

Lke this, once you enter "Yes", that variable will be put to false, and you'll jump out of the loop.就像这样,一旦你输入“是”,那个变量就会被置为假,你就会跳出循环。
The main trick with while-loops is that you always need to set the condition to true, just before you start the while-loop. while 循环的主要技巧是,您总是需要在开始 while 循环之前将条件设置为 true。

I have noticed that if the user answers "No" the program will just keep on working and not ask the question again我注意到如果用户回答“否”,程序将继续工作,不再问问题

That's what you told the program to do!这就是你告诉程序要做的事情!

If you don't want an entry of "No" to end the loop, take it out of the condition:如果您不希望输入"No"来结束循环,请将其从条件中删除:

while (response != "Yes") {

Or, use your boolean, which is a bit "cleaner" (but ultimately has the same effect):或者,使用您的 boolean,它有点“干净”(但最终具有相同的效果):

while (calculator_running) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM