Data does not persist in switch case

0

Write a program that lists the names, height, weight, cpf, and sex of some people. With the data entered, then locate a person through your CPF and print your BMI.

When I choose the Query option, the data that was entered before does not remain. What is the reason?

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

typedef struct Cadastro{
    char nome[30];
    char condicao[30];
    char sexo;
    int CPF;
    float altura;
    float peso;
    float imc;
};
void Menu(){
    printf(" ====== Menu ====== \n");
    printf("1 - Cadastrar\n");
    printf("2 - Consultar \n");
    printf("3 - Sair\n");
    printf(" ================== \n");
}

main(){
    int opcao, contUser = 0;
    do{
        Menu();
        scanf("%i", &opcao);
        system("cls");
        if(opcao == 1){
        printf("Quantos cadastros deseja realizar? ");
        scanf("%i", &contUser);
        system("cls");
        fflush(stdin);
        }
        Cadastro user[contUser];

        system("cls");
        switch(opcao){
            case 1 :
                printf(" ==== Cadastro ==== \n");
                for(int i = 0; i<contUser; i++){

                    printf(" ====== %i/%i ======\n", i+1, contUser);
                    printf("Digite o nome: ");
                    scanf("%[^\n]", user[i].nome);
                    fflush(stdin);
                    printf("Digite sexo. \nf - feminino\nm - masculino: ");
                    scanf("%c", &user[i].sexo);
                    printf("Digite o CPF: ");
                    scanf("%i", &user[i].CPF);
                    printf("Digite a altura em metros(m): ");
                    scanf("%f", &user[i].altura);
                    printf("Digite o peso em quilos(kg): ");
                    scanf("%f", &user[i].peso);
                    user[i].imc = user[i].peso/(user[i].altura*user[i].altura);

                    /*if(user[i].sexo == 'f'){
                        if(user[i].imc < 19.1){
                            user[i].condicao = {"Peso Baixo"};

                        }
                    }*/
                    fflush(stdin);
                    system("cls");
                }
                system("cls");
                break;

            case 2 :
                char qualquer;
                if(contUser == 0){
                    printf("Nenhum usuario cadastrado. Pressione qualquer tecla para voltar para o menu.");
                    fflush(stdin);
                    scanf("%c", &qualquer);
                    system("cls");
                }else{
                int consulta;
                    printf(" ==== Consulta ==== \n");
                    printf("Digite o CPF do cadastro: ");
                    scanf("%i", &consulta);

                    for(int i = 0; i<contUser; i++){
                        if(user[i].CPF == consulta){
                        printf("%f", user[i].imc);
                        }
                    }
                }
                break;

            case 3 :
                printf("Obrigado!");
                break;
        }
    }while(opcao != 3);
}

I'm using the following entry to test:

1
1
1
Nome
m
1234
1.5
22.5
2
1234
3
  

In parts:

1
1
     

I'm opening a single registration.

1
Nome
m
1234
1.5
22.5
     

I am selecting the register option, entering name Nome , sex    m , CPF 1234 , height 1.5 and weight 22.5 .

2
1234
     

I'm checking the recorded IMC for the person with% CP 1234 .

3

I'm shutting down the program.

The expected result is 10.0 , but I'm not getting this value.

I also did a test leaving case 2 just to display the first registered CPF.

case 2:
   printf("%i", user[0].CPF);
   break;

A random value other than the CPF that has been registered is displayed.

    
asked by anonymous 04.10.2017 / 04:39

1 answer

1

The main problem is the definition of the vector within do-while. This causes the vector to be initialized every time the replay occurs.

Option 1:

main(){
    int opcao, contUser = 0;
    do{
        Menu();
        scanf("%i", &opcao);
        system("cls");
        if(opcao == 1){
        printf("Quantos cadastros deseja realizar? ");
        scanf("%i", &contUser);
        Cadastro user[contUser];
        system("cls");
        fflush(stdin);
        }

Option 2:

main(){
    int opcao, contUser = 0;
    printf("Quantos cadastros deseja realizar? ");
    scanf("%i", &contUser);
    Cadastro user[contUser];
    do{
        Menu();
        scanf("%i", &opcao);
        system("cls");
        fflush(stdin);
        }
    
04.10.2017 / 23:10