简体   繁体   中英

Menu gets a default option after runing a function

Here is my code. After I run the 1st option in the menu and insert what is asked, the menu is showed but get the value "0" as default, that is the option to quit the program. If I choose the 2nd or the 3rd option, I get the printf that is inside that option and return to the menu again, with no default value. Any clue why is this happening ? I pretend to insert my data in option 1 and then keep the program running with the menu.

#include <stdio.h>
#include <stdlib.h>

#define LIMIT 30
#define MAX_STUDENTS 60
#define MENU_MIN 0
#define MENU_MAX 6
#define NUM_MIN 2150001
#define NUM_MAX 2169999
#define GRADE_MIN 0
#define GRADE_MAX 20

typedef struct
{
    int number;
    int finalGrade;
    char name[LIMIT];
} tStudent;

int check_insert (int min, int max);
int read_studant_data(tStudent vStudents[MAX_STUDENTS], int numStudents);
void show_studant_data(tStudent vStudents[MAX_STUDENTS], int numStudents);
void change_studante_grade(tStudent vStudents[MAX_STUDENTS], int numStudents);
//void mostrar_estatisticas(tStudent vStudents[], int numStudents);
//void gravar_ficheiro(tStudent vStudents[], int numStudents);
//void ler_ficheiro(tStudent vStudents[], int numStudents);
int menu ();

void main(void)
{
    tStudent vStudents[MAX_STUDENTS];
    int numStudents = 0;

    int option;

    do
    {
        option = menu();
        switch(option)
        {
        case 1:
            printf("*Insert data*\n");
            read_studant_data(vStudents, numStudents);
            break;

        case 2:
            printf("*Show*\n");
            //mostrar_dados_estudante(vStudents, numStudents);
            break;

        case 3:
            printf("*Change final grade*\n");
            //alterar_dados_aluno(vStudents, numStudents);
            break;

        case 4:
            printf("**\n");
            break;

        case 5:
            printf("**\n");
            break;

        case 6:
            printf("**\n");
            break;

        case 0:
            printf("Quit");
            break;
        }
    }
    while (option != 0);
};

int menu ()
{
    int opcao;

    do
    {
        printf("\n1 - Insert data");
        printf("\n2 - Show data");
        printf("\n3 - Change final grade");
        printf("\n4 - ");
        printf("\n5 - ");
        printf("\n6 - ");
        printf("\n0 - Quit\n\n");
        printf("Choose an option:\n");

        opcao =  check_insert(MENU_MIN, MENU_MAX);

        printf("\n");
    }

    while (opcao < MENU_MIN || opcao > MENU_MAX);
    return opcao;
};

int check_insert (int min, int max)
{
    int insert;

    do
    {
        scanf("%i", &insert);

        if ((insert < min) || (insert > max))
        {
            printf("Value out of range!\n");
            printf("Insert a new one\n");
        }
        else
        {
            return insert;
        }
    }
    while ((insert < min) || (insert > max));
};

int read_studant_data(tStudent vStudents[MAX_STUDENTS], int numStudents)
{
    int numero;
    int notaFinal;
    char nome[LIMIT];

    printf("Students number:\n");
    numero = check_insert(NUM_MIN, NUM_MAX);

    printf("\nFinal grade to the student:\n");
    notaFinal = check_insert(GRADE_MIN, GRADE_MAX);

    printf("\nStudents name:\n");
    scanf("% [^\n]s", nome);

    numStudents++;
    printf("%i", numStudents);
};

After I run the 1st option in the menu and insert what is asked, the menu is showed but get the value "0" …

This is just one of the possible outcomes, since, as user3629249 noted, in function: check_insert() , if the call to scanf() fails, then the variable insert will contain what every trash is on the stack at that location. And exactly this call to scanf() fails after read_studant_data() ran because, as also user3629249 noticed, scanf("% [^\\n]s", nome); is nonsense. Perhaps you meant: scanf(" %[^\\n]", nome); - this suggestion indeed fixes that error.
The reason for the failure is that the malformed scanf() in read_studant_data() doesn't read the student's name, but leaves it in the standard input buffer, and the subsequent scanf("%i", &insert) in check_insert() (called from menu() ) cannot convert the name to an integer.

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