如何将 Switch-Case 和 Goto 命令变成其他东西?

How to turn Switch-Case & Goto commands into something else?

我正在开发一个允许用户输入整数以从菜单中选择选项的程序。每次选择后,用户应返回菜单。我可以用 goto 完美地完成它,但我必须使用而不是 goto 命令。如何从程序中删除 goto 并使其正常运行? 我只想编程继续工作,但必须从项目中删除 goto,该怎么做?谢谢

#include <stdio.h>
#pragma warning(disable:4996)
#include <string.h>

struct student
{
    char name[50];
    char surname[50];
    char major[50];
    int idnum;
} s[100];

void sortstudent(struct student students[], int s);

int main()
{
    int i, total, id, k, mode, flag = 0;
    struct student s[100];
    printf("Enter capacity of the classroom: ");
    scanf("%d", &total);
menu:
    printf("\nType 1 to add student record\nType 2 to search a student\nType 3 to sort students");
    printf("\nType 4 to show the classroom information\nType 5 to exit the program\n");
    scanf("%d", &mode);

    switch (mode)
    {

    case 1:
    //hidden commands
        goto menu;

    case 2:
    //hidden commands
        goto menu;


    case 3:
        //hidden commands
        goto menu;

    case 4:
        //hidden commands
        goto menu;


    case 5:
        printf("Shuttuing down the program.");
        break;

    default:
        printf("Invalid value. Chose an option from the menu again: ");
        goto menu;

    }
}

void sortstudent(struct student students[100], int s)
{
    int i, j;
    struct student temp;
    for (i = 0; i < s - 1; i++)
    {
        for (j = 0; j < (s - 1 - i); j++)
        {
            if (students[j].idnum > students[j + 1].idnum)
            {
                temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}

将菜单放在函数中,并使用 while 循环重复选择。选择 5 将 return 从函数到 main 并退出。
scanf 如果输入不是整数,则会出现问题。

#include <stdio.h>
#pragma warning(disable:4996)
#include <string.h>

struct student
{
    char name[50];
    char surname[50];
    char major[50];
    int idnum;
} s[100];

void sortstudent(struct student students[], int s);
void menu(void);

int main()
{
    menu ( );
    return 0;
}

void menu(void)
{
    int i, total, id, k, mode, flag = 0;
    struct student s[100];
    printf("Enter capacity of the classroom: ");
    scanf("%d", &total);
    while ( 1)
    {
        printf("\nType 1 to add student record\nType 2 to search a student\nType 3 to sort students");
        printf("\nType 4 to show the classroom information\nType 5 to exit the program\n");
        scanf("%d", &mode);

        switch (mode)
        {
            case 1:
                //hidden commands
                break;

            case 2:
                //hidden commands
                break;

            case 3:
                //hidden commands
                break;

            case 4:
                //hidden commands
                break;

            case 5:
                printf("Shuttuing down the program.");
                return;

            default:
                printf("Invalid value. Chose an option from the menu again: ");
                break;

        }
    }
}

void sortstudent(struct student students[100], int s)
{
    int i, j;
    struct student temp;
    for (i = 0; i < s - 1; i++)
    {
        for (j = 0; j < (s - 1 - i); j++)
        {
            if (students[j].idnum > students[j + 1].idnum)
            {
                temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}

使用 fgets 和 sscanf 避免 scanf 问题

#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
#include <string.h>

struct student
{
    char name[50];
    char surname[50];
    char major[50];
    int idnum;
};

void sortstudent(struct student students[], int s);
void menu(void);

int main()
{
    menu ( );
    return 0;
}

void menu(void)
{
    char mode[100] = "";
    int i, total, id, k, flag = 0;
    int result = 0;
    struct student s[100];
    do {
        printf("Enter capacity of the classroom: ");
        if ( ! fgets ( mode, sizeof mode, stdin)) {
            fprintf ( stderr, "fgets EOF\n");
            exit ( EXIT_FAILURE);
        }
        result = sscanf(mode,"%d", &total);
    } while ( result != 1);
    while ( 1)
    {
        printf("\nType 1 to add student record\nType 2 to search a student\nType 3 to sort students");
        printf("\nType 4 to show the classroom information\nType 5 to exit the program\n");
        if ( ! fgets ( mode, sizeof mode, stdin)) {
            fprintf ( stderr, "fgets EOF\n");
            exit ( EXIT_FAILURE);
        }

        switch (mode[0])
        {
            case '1':
                //hidden commands
                break;

            case '2':
                //hidden commands
                break;

            case '3':
                //hidden commands
                break;

            case '4':
                //hidden commands
                break;

            case '5':
                printf("Shuttuing down the program.");
                return;

            default:
                printf("Invalid value. Chose an option from the menu again: ");
                break;

        }
    }
}

void sortstudent(struct student students[100], int s)
{
    int i, j;
    struct student temp;
    for (i = 0; i < s - 1; i++)
    {
        for (j = 0; j < (s - 1 - i); j++)
        {
            if (students[j].idnum > students[j + 1].idnum)
            {
                temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}