我有几个函数。基本上是一个菜单,用户可以选择1-n个不同的选项,每个选项都有一个与它们相关的功能。在这个例子中,它被愚弄了。干净地退出C函数
我想确定的是过早退出函数的最佳方法。例如,当用户在菜单选项的功能中按下Enter时,我希望程序将它们发送回菜单,而不在该功能中运行任何其他功能。
在以下情况下,我只需拨打showMenu()
并在其后放置return
声明。唯一的问题是,如果用户退出多个函数,那么会有一系列返回语句需要在最后解开。
有人请告诉我是否有更高效的方法来实现这一点,或者我是否有钱。
void showMenu()
{
//Display menu
//Prompt user for menu option
//Run function of appropriate menu option
runSelectedFunction();
}
void runSelectedFunction()
{
//Get user input for the function and validate
//Check if the user input was only a '\n' if so show the menu and exit
showMenu();
return;
//Do the stuff that this function is meant to do.
}
单入口单出口使程序更容易阅读。 – 2011-04-05 05:37:22