Search code examples
cstructswitch-statementgoto

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


I was working on a program which allows user to put integers to choose an option from the menu. After every choice, user should go back to the menu. I could do it perfectly with goto, but I have to use rather than goto command. How I can remove goto from the program and keep it working well? I just want to program keep working but goto must be removed from the project, how to do that? Thanks.

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

Solution

  • Put the menu in a function and use a while loop to repeat the selection. Choosing 5 will return from the function to main and exit.
    scanf is a problem if a input is not an integer.

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

    Use fgets and sscanf to avoid scanf problems

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