繁体   English   中英

c中的返回功能主菜单

[英]return function main menu in c

我将从本周开始编码,因此对此很无聊。 我需要有关在脚本中返回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();
}

请帮我写返回代码行,谢谢

这是一些代码,可以帮助您了解函数返回值的机制,在这种情况下,可以返回主函数。

至于一些建议,请仔细阅读有关魔术数字,特别是为什么它们不好的原因。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM