gpt4 book ai didi

c++ - 当 if 语句告诉它在 main 中返回 0 时,我的程序没有退出

转载 作者:行者123 更新时间:2023-11-30 02:13:41 25 4
gpt4 key购买 nike

这里是 C++ 菜鸟,我正在制作这款基于文本的 RPG 类游戏,在标题屏幕上,有一个退出选项。这在 main() 中编码为:

int main() {
bool main_menu = true;
while(main_menu == true) {
cout <<"\033[2J\033[1;1H";
cout <<GRN"Welcome to Yhawn! The text RPG straight outta 1972.\n";
cout <<"\n";
cout <<"(N)ew\n(Q)uit\n";
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() 函数。

关于c++ - 当 if 语句告诉它在 main 中返回 0 时,我的程序没有退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58924705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com