gpt4 book ai didi

c++ - 到达 void 函数的结尾会产生意想不到的结果

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:18 27 4
gpt4 key购买 nike

我写了一个简单的程序来管理姓名列表(下面是程序的一部分)。我希望函数“choice()”结束并返回到 main()——从而结束程序——当用户对变量“option”的输入为 4 时。然而,choice() 往往会重复几次即使在将 4 分配给“选项”之后,例如:

Would you like to:    (1) - Add name(s) to the list    (2) - Remove name(s) from the list    (3) - View the list    (4) - Exit the program(type 1, 2, 3, or 4): 4Exiting Program...Would you like to:    (1) - Add name(s) to the list    (2) - Remove name(s) from the list    (3) - View the list    (4) - Exit the program(type 1, 2, 3, or 4): 4Exiting Program...ryan$ _

In the absence of the entire program, what do you think may be causing the problem? Sorry if there is not enough information to make a recommendation.

You can see I commented "exit(0)" from the end of the fourth if statement of choice()--in which choice() should reach its end and return to main(). I would prefer to avoid this statement but it is the only solution I can make work at the moment. Thanks for your help.

-Ryan

int main()
{
cout << endl
<< "WELCOME TO THE NAME LIST PROGRAM" << endl
<< "--------------------------------" << endl;

choice();

return 0;
}


void choice()
{
string option = "0";

cout << "\nWould you like to:" << endl
<< "\t(1) - Add name(s) to the list" << endl
<< "\t(2) - Remove name(s) from the list" << endl
<< "\t(3) - View the list" << endl
<< "\t(4) - Exit the program" << endl
<< "(type 1, 2, 3, or 4): ";

getline(cin,option);

while( option != "1" && option != "2" &&
option != "3" && option != "4" )
{
cout << "\nInvalid input, reenter: ";
getline(cin,option);
}

if(option == "1")
{
appendNames();
choice();
}
else if(option == "2")
{
string flag = "menu";
removeNames(flag);
choice();
}

else if(option == "3")
{
viewNames();
choice();
}

else if(option == "4")
{
cout << "\n\nExiting Program...\n\n";
// exit(0);
}
}

最佳答案

我认为问题来自对函数工作方式的误解。假设您有一个显示您姓名的函数:

void showName()
{
cout << "Ryan" << endl;
}

现在假设您想重复 10 次。在您特定的编程方式中,您可能会做如下事情:

void showName(int times)
{
cout << "Ryan" << endl;

if ( times > 0 ) {
showName( --times );
}

return;
}

int main()
{
showName( 10 );
}

这种编程方式称为递归,但我很确定您是偶然接触到的。递归在某些情况下很方便,但它不是做简单事情的常用方法,比如重复某事。让我们看看调用该函数 10 次的实际程序:

void showName()
{
cout << "Ryan" << endl;
}

int main()
{
for(int i = 0; i < 10; ++i) {
showName();
}
}

每次调用函数 showName() 时,被调用方都必须返回给调用方。如果您执行示例中显示的操作:

if( option == "1" )
{
appendNames();
choice();
}

然后,您实际上调用了 choice() 的次数与用户选择除 4(退出)之外的任何其他选项的次数相同。这解释了为什么您必须调用 exit( 0 ) 才能立即生效。

总而言之,您的程序应该是:

int main()
{
cout << endl
<< "WELCOME TO THE NAME LIST PROGRAM" << endl
<< "--------------------------------" << endl;

while( !choice() );

return 0;
}


bool choice()
{
bool toret = false;
string option = "0";

cout << "\nWould you like to:" << endl
<< "\t(1) - Add name(s) to the list" << endl
<< "\t(2) - Remove name(s) from the list" << endl
<< "\t(3) - View the list" << endl
<< "\t(4) - Exit the program" << endl
<< "(type 1, 2, 3, or 4): ";

getline(cin,option);

while( option != "1" && option != "2" &&
option != "3" && option != "4" )
{
cout << "\nInvalid input, reenter: ";
getline(cin,option);
}

if(option == "1")
{
appendNames();
}
else if(option == "2")
{
removeNames();
}

else if(option == "3")
{
viewNames();
}

else if(option == "4")
{
cout << "\n\nExiting Program...\n\n";
toret = true;
}

return toret;
}

希望这对您有所帮助。

关于c++ - 到达 void 函数的结尾会产生意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21657114/

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