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
, sexm
, CPF1234
, height1.5
and weight22.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?
– Jefferson Quesado
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.
– Nivaldo Junior
Yes. Then I fill in the normal data.
– Nivaldo Junior
So you type in
1\n1\n1\nNome\nm\n1234\n\n1.7\n80.4\n
?– Jefferson Quesado
Yes. When I go to the case 2 by that CPF that was inserted, another number appears.
– Nivaldo Junior
Certain height
1.5
and weight22.5
, do you always get different values? Never10.0
?– Jefferson Quesado
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.
– Nivaldo Junior
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
, height1,5
and weight22.5
, what comes out printed when I consult it is10.0
or print random values?– Jefferson Quesado
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
– Jefferson Quesado
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.
– Nivaldo Junior
I did a test leaving the case code 2 only to display the CPF that was registered and appears a random value.
– Nivaldo Junior
Use the malloc function of <stdlib. h> to allocate the memory needed for your user structure array.
– Anonimo
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
– Jefferson Quesado
@Anonymity, in this case is not necessary, it is using automatic allocation for such effect, but just can not regret the count of entries
– Jefferson Quesado
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
@Anonimo, see https://answall.com/a/215832/64969; automatic allocation
– Jefferson Quesado
@Nivaldo, I noticed you use
typedef struct Cadastro {...};
, and in my memory, mixing structure statement withtypedef
follows the formattypedef 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– Jefferson Quesado
I’m saving as cpp.
– Nivaldo Junior