c中的返回功能主菜单

时间:2016-03-10 13:23:20

标签: c menu return main

我本周开始编码,所以我对它进行了假设。我需要有关在脚本中返回main的帮助。例如,当我完成课程注册部分时,我无法返回菜单程序崩溃

代码:

#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();
}

请帮我写回程代码行,先谢谢

1 个答案:

答案 0 :(得分:0)

这里有一些代码可以帮助你理解函数返回值的机制,在你的情况下回到main函数。

至于一些建议,请阅读魔术数字,特别是它们为什么不好。

/*
 *  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);
}