如何把Switch-Case & Goto命令变成别的东西?

问题描述 投票:0回答:1

我正在做一个程序,它允许用户从菜单中输入整数来选择一个选项。每次选择后,用户应该回到菜单中。我可以用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;
            }
        }
    }
}
c struct switch-statement goto
1个回答
1
投票

把菜单放在一个函数中,用一个while循环来重复选择。选择 5 将从函数返回到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;
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.