Data does not persist on switch case

Asked

Viewed 238 times

0

Write a program that registers the name, height, weight, Cpf and sex of some people. With the data registered, then locate a person through your CPF and print your IMC.

When I choose the Query option the data that was entered before does not remain. Why?

    #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

Split:

1
1

I’m opening up a single record.

1
Nome
m
1234
1.5
22.5

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

2
1234

I’m checking the recorded BMI for the person with social security number 1234.

3

I’m shutting down the program.

The expected result is 10.0, but I’m not getting that value.

I also made a test leaving case 2 only to display the first registered CPF.

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

Exit a random value different from the CPF that was registered.

  • Can you help me? What are the steps you have taken to simulate the problem?

  • At the first execution I choose option 1 to register, then type 1 again to do only 1 registration. When you go back to the menu I choose 2 to query and type in the CPF. I already ran a test in case 2 just to print the user[0]. CPF and memory junk appears.

  • Yes. Then I fill in the normal data.

  • So you type in 1\n1\n1\nNome\nm\n1234\n\n1.7\n80.4\n?

  • Yes. When I go to the case 2 by that CPF that was inserted, another number appears.

  • Certain height 1.5 and weight 22.5, do you always get different values? Never 10.0?

  • I did not understand the question. The problem I am finding is in the second menu option. After I register and consult, the registered values are lost.

  • I’m editing your question to add your operation so that it’s viable for someone to try to reproduce. For this, I need to know if, for a CPF person 1234, height 1,5 and weight 22.5, what comes out printed when I consult it is 10.0 or print random values?

  • I did the editing, could you check if what I put in is consistent with what you’re really doing? I wrote this based on your responses to the comments

  • That’s right. But in case 2 does not give the right BMI result because the user value[i]. CPF is different from the one entered.

  • I did a test leaving the case code 2 only to display the CPF that was registered and appears a random value.

  • Use the malloc function of <stdlib. h> to allocate the memory needed for your user structure array.

  • If you can put how you did the test (the template of what I put as editing), it would be more info for the question

  • @Anonymity, in this case is not necessary, it is using automatic allocation for such effect, but just can not regret the count of entries

  • Remember that the value of the contUser variable is not known at compile time. The correct way to make dynamic allocation is to use the appropriate functions for this.

  • @Anonimo, see https://answall.com/a/215832/64969; automatic allocation

  • @Nivaldo, I noticed you use typedef struct Cadastro {...};, and in my memory, mixing structure statement with typedef follows the format typedef struct _cadastro {...} Cadastro;, with the name of the new type set at the end. This in pure C, not C++. What language are you using? Perhaps this definition of the way it is might confuse the compiler

  • I’m saving as cpp.

Show 13 more comments

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 repetition 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);
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.