简体   繁体   English

当模块化 C 代码时,在一个函数内有没有办法循环到另一个函数(即到我的 main() c 文件)?

[英]When modularising C code, within a function is there a way to loop to another function (i.e. to my main() c file)?

I'm in my first semester at university studying C programming.我在大学的第一个学期学习 C 编程。 We've gained an introduction to modularising our code, and the basic gist of my problem is that I've created a function which prompts the user to continue or exit the program.我们已经获得了模块化代码的介绍,我的问题的基本要点是我创建了一个提示用户继续或退出程序的函数。 Obviously I have a few other functions before this and I call these in main().显然,在此之前我还有其他一些函数,我在 main() 中调用它们。 Bear in mind the functions are in separate C files.请记住,这些函数位于单独的 C 文件中。 I wish to loop back to main() if the user inputs 'Y' (ie to start the program from the beginning), but given my beginner knowledge I've had a hard time figuring out how to go about this.如果用户输入“Y”(即从头开始启动程序),我希望循环回到 main(),但鉴于我的初学者知识,我很难弄清楚如何去做。 Any help/guidance would be appreciated!任何帮助/指导将不胜感激!

int continueOrExit() {
    char response; 
    char upperResponse;
    printf("Do you wish to continue the program? Y/N");
    scanf("%c", &response);
    upperResponse = toupper(response);

    while(upperResponse == 'Y')
        main(); // this is the bit which I struggle with

    .....
    }

You should not run your main() again from inside a function already called from main() .你不应该运行main()从从已经调用函数内部再次main() Well, you could;好吧,你可以; recursive calls are possible, but you definitely don't want to do it in this case.递归调用是可能的,但在这种情况下你绝对不想这样做。

What you want to do is for your continueOrExit() to return true when user chooses to continue, and put a selected large part of your main() body in a loop:您想要做的是让continueOrExit()在用户选择继续时返回true,并将main()主体的选定部分放入循环中:

do
{
    /* your code here */
} while (continueOrExit());

Going from one function directly to another is against the rules of structured programming .从一个函数直接转到另一个函数违反了结构化编程的规则。 It may be used for handling of errors ( longjmp ), but such a mechanism should be used sparingly - only when you really have an error which you cannot handle in any other way.它可用于处理错误 ( longjmp ),但应谨慎使用这种机制——仅当您确实遇到了无法以任何其他方式处理的错误时。

To implement the "start over" functionality, you have to structure your code with that in mind.要实现“重新开始”功能,您必须考虑到这一点来构建您的代码。 For example:例如:

int exitByUserRequest()
{
    char upperResponse;
     ...
    return (upperResponse != 'Y');
}

int main()
{
    do
    {
        ...
    } while (!exitByUserRequest());
}

Here, the main function calls exitByUserRequest and interprets its exit code.在这里, main函数调用exitByUserRequest并解释其退出代码。

If you have several layers of functions between main and "ask user whether to continue", all of them should pass the information "exit or continue" to the caller, using return codes.如果在main和“询问用户是否继续”之间有多层函数,它们都应该使用返回码将“退出或继续”信息传递给调用者。 Therefore, you should avoid this situation - your "exit or continue" should be called directly from main .因此,您应该避免这种情况——您的“退出或继续”应该直接从main调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 斐波那契函数在C中具有大数字(即1000个数字) - Fibonacci function with big number (i.e. 1000 digits) in C 为什么我的用于将元素插入到哈希树中的C代码在Main()中起作用,但是当我通过函数调用它时却不起作用? - Why does my C code for inserting an element into a hash tree work in Main() but not when I call it via function? (C代码)我如何在函数之间传递全局变量,并在主函数需要它们时返回它们? - (C code) how would I pass my global variables between functions and return them when the main function needs them also? 如何在C中将另一个文件中的函数调用为主函数? - How to call a function from another file into main function in c? 为什么我在 C 中的 main 函数只打印第一个 for 循环? - Why does my main function in C only prints the first for loop? C-将函数fwrite()放入循环时,写入文件失败 - C - write to file faild when I put function fwrite() in a loop 什么时候默认为C中的主要功能 - When to default to main function in C 试图用我的主文件在 c 和 function 中写入文件,我可以制作文件,但里面什么都没有 - Trying to write a file in c with a function with my main, I can make the file but nothing is in it 从C中的另一个函数调用main函数 - Calling main function from another function in C 如何拒绝字母数字命令行参数(即 ./main 20x),而不是纯数字参数(即 ./main 20)? (在 C 中) - How can I reject an alphanumeric command-line argument (i.e. ./main 20x), but not a solely numeric one (i.e. ./main 20)? (in C)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM