1
Hello, I want a help I am making a program that has a menu the registered user and then consult the registration, the problem and I am not being able to consult the "database" vector of structs.
The user registers and redirects to the menu with a complete registration message, right after he chooses to consult his registration the program asks the name of the user and uses the library string to see if the registration exists at first the idea and only printa on the screen "existing user" then increment the rest.
but when the user type the name he and automatically redirected to the function Exit() BECAUSE?
#include <stdlib.h>
#include <string.h>
#define RET 0
int qc = 1;
void menu(void);
int cadastrar(void);
void consultar(void);
void sair(void);
struct usuarios{
char nome[30];
int idade;
int user;
};
int main(void)
{
menu();
return(RET);
}
void menu(void)
{
int esc;
retorno:
printf("------------------------------------\n"
"| Sistema de cadastro de cliente |\n"
"------------------------------------\n");
printf("====MENU=======\n"
"1.Cadastrar |\n"
"2.Consultar |\n"
"0.Sair |\n");
printf("===============\n\n");
printf("opção: ");
scanf(" %d", &esc);
switch(esc){
case 1:
cadastrar();
case 2:
consultar();
case 0:
sair();
default:
system("clear");
printf("Digito invalido\n\n");
goto retorno;
}
}
int cadastrar()
{
int ind;
system("clear");
printf("------------------------");
printf("\n| Cadastro de usuarios |\n");
printf("------------------------\n");
printf("Quantos usuarios quer cadastrar? ");
scanf("%d", &qc);
struct usuarios info_user[qc];
struct usuarios *ptr[qc];
system("clear");
for(ind=0;ind<qc;ind++){
ptr[ind] = &info_user[ind];
ptr[ind]->user = 1;
}
for(ind=0;ind<qc;ind++){
ptr[ind] = &info_user[ind];
printf("-------------------------\n");
printf("| Cadastrando usuario |\n");
printf("-------------------------\n");
printf("Digite seu nome: ");
scanf("%s", ptr[ind]->nome);
printf("Digite sua idade: ");
scanf("%d", &ptr[ind]->idade);
ptr[ind] -> user++;
system("clear");
}
if(qc == 1){
printf("Usuario cadastrado com sucesso!\n\n");
}
else if(qc > 1){
printf("Usuarios cadastrados com sucesso!\n\n");
}
menu();
return(RET);
}
void consultar()
{
char nomeu[30];
int comp;
int ind;
struct usuarios info_user[qc];
struct usuarios *ptr[qc];
system("clear");
printf("-------------------------\n"
"| Consultando usuario |\n"
"-------------------------\n");
printf("\nDigite o nome do usuario: ");
scanf("%s", nomeu);
for(ind=0;ind<qc;ind++){
ptr[ind] = &info_user[ind];
}
for(ind=0;ind<qc;ind++){
comp = strcmp(nomeu, ptr[ind] -> nome);
if(comp == 1){
printf("User existente");
}
}
}
void sair()
{
printf("Encerrando programa...");
exit(0);
}```
Review the operation of the switch/case command. The way you did if you were informed 1 it will run:
cadastrar();
followed byconsultar();
followed bysair();
followed bysystem("clear"); printf("Digito invalido\n\n"); goto retorno;
and, I suspect you don’t want to perform all this. Another thing to study is the scope of variables. You statedstruct usuarios
with global scope but declaredinfo_user[qc]
with local scope to function, that is when leaving the functioncadastrar
this array will cease to exist. Inconsultar
your array has no data (it actually has memory junk).– anonimo
Another thing: I didn’t understand the reason for using an array of pointers to
struct usuarios
, perhaps a confusion of concepts.– anonimo