简体   繁体   中英

return function main menu in c

I'm starting coding this week so I'm dummy about it. I need help about return to main in my script. For example when I done Course registration part I can't return menu program is crashing

Codes:

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

Pls help me write return code lines, Thanks in Advance

Here is some code that may help you understand how to the mechanics of functions returning values, in your case back to the main function.

As for some advice, please read up about magic numbers and specifically why they are bad.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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