c - c中的返回函数主菜单

标签 c menu return program-entry-point

我这周就要开始编码了,所以我对此很茫然。我需要有关在脚本中返回 main 的帮助。例如,当我完成类(class)注册部分时,我无法返回菜单程序崩溃

代码:

#include <stdafx.h>
#include <stdio.h>

void eng();
void menu();
void huh();

int main()
{
    menu();

    return 0;
}

void menu()
{
    int menu1choice;

    printf("Menu\n");
    printf("\n");
    printf("1. Student Registration\n");
    printf("2. Show Students.\n");
    printf("Please enter number: ");
    scanf_s("%d", &menu1choice);
    switch (menu1choice)
    {
        case 1:
        {
            eng();
            break;
        }

    }
}

void eng()
{
    int a = 5;
    char name[30];

    printf("1.Student Number: ");
    scanf_s("%d", &a);
    //student number
    printf("2.Name: ");
    scanf_s("%s", &name);
    //student name
    getchar();
}

void huh()
{
    int a = 5;
    char name[30];

    printf("Your Student number: %d\n", a);
    printf("Your Name: %s\n", name);
    //result
    getchar();
}

请帮我写返回代码行,提前致谢

最佳答案

这里有一些代码可以帮助您了解如何返回值的函数的机制,在您的情况下返回到主函数。

至于一些建议,请阅读有关魔数(Magic Number)的内容,特别是它们不好的原因。

/*
 *  35917794_main.c
 */

#include <stdio.h>

#define     STU_REG         1
#define     STU_SHOW        2
#define     EXIT_SUCCESS    0

unsigned int show_menu(void);

unsigned int main
        (
        unsigned int    argc,
        unsigned char   *arg[]
        )
{
    unsigned int    menu1choice;
/*
 *  The next statements says run the function show_menu() and put the returned
 *  result in the variable menu1choice.
 */
    menu1choice = show_menu();
    switch(menu1choice)
        {
        case (STU_REG):
            {
            printf("\nGo do STUDENT REGISTRATION things...\n\n");
            break;
            }
        case STU_SHOW:
            {
            printf("\nGo do SHOW STUDENT things...\n\n");
            break;
            }
        default:
            {
            printf("\nGo do something for invalid option...\n\n");
            break;
            }
        }
    return(EXIT_SUCCESS);
}


unsigned int show_menu
        (
        void
        )
{
    unsigned int    ui_W0;
    printf("Menu\n\n");
    printf("1. Student Registration\n");
    printf("2. Show Students.\n");
    printf("Please enter number: ");
    scanf("%d", &ui_W0);
/*
 *  The next statements says run the function show_menu() has finished and returned
 *  returns the result in the variable ui_W0.
 */
    return(ui_W0);
}

关于c - c中的返回函数主菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35917794/

相关文章:

c - 嵌套结构的范围是什么?

javascript - 从一个 JavaScript 函数调用另一个具有 return false 的 JavaScript 函数;?

java - 为什么这个返回的数组不起作用?

c - C 共享库中的函数重载(不同的返回类型,不同数量的参数)

c++ - C/C++ : What is the difference between a statically-linked library and an object file?

c - 分配未初始化的 void* 指针

seo - 下拉菜单和seo

python - 菜单要加多少?

php - 基于数据库中的数据在 PHP 中动态构建菜单太慢

c - 如何在c中返回char数组指针。