do ... while 循环不破坏 c++
do ... while loop not breaking c++
目前我在执行此 do ... 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;
}
网上查了一下,如何跳出do ... while循环,条件为真时再循环,条件为假时退出。
因此,如果您查看代码,如果用户输入 no,它会设置 exit = false,这会将它从更大的 do while 循环中取出,而 break 会将它从当前的 do while 循环中取出。
如果用户输入 yes,它会将 exit 更改为 true,这会使其跳出当前的 do ... while 循环,但不会跳出第二个循环。
我的问题是,(或者我需要帮助的)是当用户输入 'no' 时,它无法退出 do ... while 循环,我很困惑为什么。 (它循环回到程序的开头。)
在(缩短的)代码中
do
{
bool exit = false;
// ...
} while (!exit);
您实际上有 两个不同的 符号,名为 exit
。在循环中你有变量。在循环之外,用于条件的函数 std::exit
。如果你有 using namespace std;
.
,这将是普通的 exit
函数 exit
在条件中使用时会衰减为指向函数的指针,并且永远不会是 "false"。所以条件 !exit
总是 true
并且你有一个无限循环。
要解决这个问题,您需要做两件事:
了解到using namespace std;
is very bad practice
将要定义的变量exit
移到循环外。而且你真的应该重命名为更具描述性的东西("exit"这个词有点笼统)。
我认为@SomeProgrammerDude 给出了非常值得遵循的优秀建议——但我会更进一步,并建议将代码移动到一个单独的函数中以获取用户的响应,这样您就可以更轻松地推理每个隔离部分代码:
bool check_for_exit() {
std::string prompt = "\nDo you want to exit the program? ";
std::string strexit;
do {
std::cout << prompt;
std::getline(std::cin, strexit);
prompt = "\nPlease enter yes or no";
} while (strexit != "yes" && strexit != "no");
return strexit == "yes";
}
然后在执行实际工作的代码中使用该函数,顺序如下:
do {
whatever();
} while (!check_for_exit());
在我看来,这种方法有助于避免您在代码中遇到的许多问题。
目前我在执行此 do ... 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;
}
网上查了一下,如何跳出do ... while循环,条件为真时再循环,条件为假时退出。 因此,如果您查看代码,如果用户输入 no,它会设置 exit = false,这会将它从更大的 do while 循环中取出,而 break 会将它从当前的 do while 循环中取出。 如果用户输入 yes,它会将 exit 更改为 true,这会使其跳出当前的 do ... while 循环,但不会跳出第二个循环。
我的问题是,(或者我需要帮助的)是当用户输入 'no' 时,它无法退出 do ... while 循环,我很困惑为什么。 (它循环回到程序的开头。)
在(缩短的)代码中
do
{
bool exit = false;
// ...
} while (!exit);
您实际上有 两个不同的 符号,名为 exit
。在循环中你有变量。在循环之外,用于条件的函数 std::exit
。如果你有 using namespace std;
.
exit
函数 exit
在条件中使用时会衰减为指向函数的指针,并且永远不会是 "false"。所以条件 !exit
总是 true
并且你有一个无限循环。
要解决这个问题,您需要做两件事:
了解到
using namespace std;
is very bad practice将要定义的变量
exit
移到循环外。而且你真的应该重命名为更具描述性的东西("exit"这个词有点笼统)。
我认为@SomeProgrammerDude 给出了非常值得遵循的优秀建议——但我会更进一步,并建议将代码移动到一个单独的函数中以获取用户的响应,这样您就可以更轻松地推理每个隔离部分代码:
bool check_for_exit() {
std::string prompt = "\nDo you want to exit the program? ";
std::string strexit;
do {
std::cout << prompt;
std::getline(std::cin, strexit);
prompt = "\nPlease enter yes or no";
} while (strexit != "yes" && strexit != "no");
return strexit == "yes";
}
然后在执行实际工作的代码中使用该函数,顺序如下:
do {
whatever();
} while (!check_for_exit());
在我看来,这种方法有助于避免您在代码中遇到的许多问题。