当if语句告诉程序返回0时,我的程序不会退出

My program does not quit when the if statement tells it to return 0 in main

本文关键字:程序 我的 退出 0时 返回 if 语句      更新时间:2023-10-16

C++菜鸟,我正在制作这个基于文本的RPG游戏,在标题屏幕上,可以选择退出。这在main((中编码为:

int main() {
bool main_menu = true;
while(main_menu == true) {
cout <<"33[2J33[1;1H";
cout <<GRN"Welcome to Yhawn! The text RPG straight outta 1972.n";
cout <<"n";
cout <<"(N)ewn(Q)uitn";
cout <<"> ";
std::string main_menu_select;
std::cin >>main_menu_select;
if(main_menu_select == "N" || main_menu_select == "n") {
createNewCharacter();
main_menu = false;
// error is here
if(main_menu_select == "Q" || main_menu_select == "q") {
main_menu = false;
return 0;
}

但是,在标题屏幕上,它不将qQ作为输入,而是循环返回。这让我很困惑,因为N工作得很好,并且有相同的代码。

谢谢你的帮助。

好吧,编写的quit()函数完全没有任何作用。其中的语句return 0;并没有结束整个程序,只是结束了这个(否则为空(函数。

最简单的方法是用return 0;(如(代替对该函数的调用

if(main_menu_select == "Q" || main_menu_select == "q") {
//quit();
//main_menu = false;
return 0;
}

当然,您可以删除我注释掉的行以及无用的quit()函数。

正确制表的重要性在以下代码中显而易见:

if(main_menu_select == "N" || main_menu_select == "n") {
createNewCharacter();
main_menu = false;
// error is here
if(main_menu_select == "Q" || main_menu_select == "q") {
main_menu = false;
return 0;
}

这是选项卡上的代码:

if(main_menu_select == "N" || main_menu_select == "n")
{
createNewCharacter();
main_menu = false;
// error is here
if(main_menu_select == "Q" || main_menu_select == "q") 
{
main_menu = false;
return 0;
}

问题变得很明显:N条件中缺少闭合"}",导致Q条件在N条件中,这是不可能触发的。