-1
I’m having second thoughts about the registration function. In the part where I inform dynamically through a counter the number already registered, when it arrives in the second iteration, the variable that I passed for reference qtd
assumes a totally random value (memory junk I think). The strange thing is that when compiling the program several times for testing, I noticed that sometimes it kept the correct value in the variable, but most of the time it assumed a totally random value. I wonder if this has anything to do with pointers, because I haven’t studied this concept yet. Follow the photo of the execution and the code below. If anyone can help me I’d be grateful.
void cadastro(struct ficha_pessoa *cadp, int *qtd, int *menu)
{
char opt;
int i,cont;
//cont = *qtd;
if (*menu == 0)
{
do
{
printf("Deseja cadastrar(s/n):");
scanf("%c", &opt);
fflush(stdin);
system("cls");
}while(opt != 's' && opt != 'S' && opt != 'n' && opt != 'N');
if (opt == 's' || opt == 'S')
{
do
{
printf("Digite a Quantidade de Cadastros (Max:10):");
scanf("%d", *(&qtd));
//scanf("%d", *(&qtd));
fflush(stdin);
system("cls");
}while(*qtd > 10);
for (i=0; i < *qtd; i++)
{
printf("Quantidade de Cadastros: %d/%d \n", i , *qtd); //<- Problema aqui.
printf("Nome:");
scanf("%s", cadp[i].nome);
fflush(stdin);
printf("Idade:");
scanf("%d", &cadp[i].idade);
fflush(stdin);
printf("Altura:");
scanf("%f", &cadp[i].altura);
fflush(stdin);
printf("Peso:");
scanf("%f", &cadp[i].peso);
fflush(stdin);
//cont++;
system("cls");
//fflush(stdin);
}
*menu = 1;
printf("Quantidade Máxima de registros atingida: %d\n\n", *qtd);
}
}
else if (*menu != 0)
{
printf("O cadastro ja foi efetuado!");
system("pause");
system("cls");
}
}
post the entire program. There’s no point in a piece for someone who wants to help you. You don’t even have the statements of the variables. And understand that your register is people. It’s much simpler if you build a register as a file collection_person...
– arfneto
In function,
qtd
is a pointer (as it has been declared asint *qtd
), then to read just doscanf("%d", qtd);
(forscanf
shall be provided with a pointer). Moreover, if "you have not yet studied this concept", my sincere advice - without irony - is that you first study the subject and then program. Doing the opposite doesn’t usually work out... Taking advantage, don’t usefflush(stdin)
, because it is not portable (it may even "work" in some environments, but it is not recommended: https://stackoverflow.com/q/2979209). Other interesting reading: https://answall.com/q/42981/112052– hkotsubo
Thank you very much for the feedbacks, hkotsubo and arfneto. I still have little knowledge in programming and with these tips I acquire a lot of additional knowledge. As soon as possible I will post the full code here for a better analysis of my 'inexperience' hehehe.
– Ramiro Arthur Bayer Martins
If you are going to put your code and ask them to analyze it, I suggest you do it on time (no generic requests like "see what you can improve", and yes one or another specific item: "the X part that makes Y is in such a problem, etc"). But first I suggest you read here and here
– hkotsubo