A
Come on, buddy. I'm in college too, and I understand your euphoria, but a hint of who's already caught in life: be specific. Culting the library <string.h> by error in your code can cost you a place in the sun here in this great community. Returning to your quest, I will quote a few points:Immediately check the command gets(). It is discontinued in c and has already been replaced by fgets(). A simpler alternative to fgets is you use a scanf() with a getchar() later, so that the enter (the famous line break) does not cross your code. Example: char textoDigitado[100];
printf("Digite seu texto de até 100 caracteres: "):
scanf("%s",&textoDigitado);
getchar(); /* colocando esta funcao no final, o proximo scanf que voce usar
nao pegará o enter. */
I tried to understand why you cleaned the buffer at various times of the code with the command "fflush(stdin)", but I realized that it has no sense at all. Please remove all from your code immediately.Your user and password read and compare command is somewhat fragile. The error cited by you in the topic statement happened because of the command below:while(fscanf(buser,"%s",r) !=EOF){
sesim = strcmp(r,SenhaVrfc);
if(sesim == 0){
aux2 = 1;
break;
}
}
r is a char type pointer and Senhavrfc is an integer, i.e. it is not possible to compare a character with an integer. optimizing both user comparison and password whiles, your code would look like this: // VERIFICANDO O USUARIO NO BANCO DE DADOS.
char usuarioBanco[100];
while(fscanf(buser,"%s",usuarioBanco) !=EOF) {
if (strcmp(usuarioBanco,UserVrfc) == 0) {
aux1 = 1;
break;
}
}
// VERIFICANDO A SENHA NO BANCO DE DADOS.TXT
int senhaBanco;
while(fscanf(buser,"%s %d",r,&senhaBanco) !=EOF) {
if ((strcmp(r,"Senha:") == 0) && (senhaBanco == SenhaVrfc)) {
aux2 = 1;
break;
}
}
Your code, with the modifications I propose, will be like this:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Cadastro de novo usuário
void Cadastro(char Nome[100], char User[15], char Email[50], char Sexo[10], int Idade, int CPF, int Senha) {
FILE *buser;
buser = fopen("BancodeUsuarios.txt","w");
printf("----------Cadastro----------\n");
printf("Nome: ");
scanf("%s", Nome);
getchar();
printf("Email: ");
scanf("%s", Email);
getchar();
printf("Idade: ");
scanf("%i", &Idade);
getchar();
printf("Sexo: ");
scanf("%s", Sexo);
getchar();
printf("CPF: ");
scanf("%i", &CPF);
getchar();
printf("Nome de usuario: ");
scanf("%s", User);
getchar();
printf("Senha: ");
scanf("%i", &Senha);
getchar();
//Colocando o conteudo no BancodeUsuarios.txt
fprintf(buser,"-----------------------------------");
fprintf(buser,"Nome: %s\n", Nome);
fprintf(buser,"Email: %s\n", Email);
fprintf(buser,"Idade: %i anos\n",Idade);
fprintf(buser,"Sexo: %s\n", Sexo);
fprintf(buser,"CPF: %i\n\n", CPF);
fprintf(buser,"Nome de usuario: %s\n", User);
fprintf(buser,"Senha: %i\n\n", Senha);
}
//Login
void Login(char UserVrfc[15], int SenhaVrfc) {
//Par opção de Cadastro
char Nm[100], Us[15], Mail[50], Sx[10];
int Idad, Sen, C_P_F;
//Variaveis auxiliares
char r[1000];
int aux1, aux2;
int resp;
printf("------------Login------------\n");
printf("Usuario: ");
scanf("%s", UserVrfc);
getchar();
printf("Senha: ");
scanf("%i", &SenhaVrfc);
getchar();
FILE *buser;
buser = fopen("BancodeUsuarios.txt","r");
//Verifica o nome de usuário
aux1 = 0;
char usuarioBanco[100];
while(fscanf(buser,"%s",usuarioBanco) !=EOF) {
if (strcmp(usuarioBanco,UserVrfc) == 0) {
aux1 = 1;
break;
}
}
fclose(buser);
//Verifica a senha
buser = fopen("BancodeUsuarios.txt","r");
aux2 = 0;
int senhaBanco;
while(fscanf(buser,"%s %d",r,&senhaBanco) !=EOF) {
if ((strcmp(r,"Senha:") == 0) && (senhaBanco == SenhaVrfc)) {
aux2 = 1;
break;
}
}
//Verificação para login
fclose(buser);
if(aux1 == 1 && aux2 == 1) {
printf("Bem-vindo!\n");
} else if(aux1 == 1 && aux2 == 0) {
printf("Senha incorreta\n");
} else {
printf("Usuário inexistente, digite 1 para se cadastrar ou 2 para sair\n");
if(resp == 1) {
system("cls");
Cadastro(Nm, Us, Mail, Sx, Idad, C_P_F, Sen);
}
}
}
int main() {
//variaveis de cadastro
char Nm[100], Us[15], Mail[50], Sx[10];
int Idad, Sen, C_P_F;
//variaveis de login
char Usie[15], Snh;
//variaveis auxiliares
int resp;
//Criando banco de usuários
printf("----------PetOnline------------\n");
printf("Bem-vindo ao seu Petshop Online!\n");
printf("Somos uma petshop de bem com mundo, que propoe vender produtos\n");
printf("que sejam adequados para seu animalzinho e ainda ajudar o mundo\n");
printf("e aqueles que precisam, com suas doacoes montamos campanhas de\n");
printf("castracao e ajudamos animais em situacao de rua ou que necessitem");
printf("de ajuda! Entao muito obrigada!\n");
printf("Se for novo aqui, digite 1 para se cadastrar e fazer parte desse\n");
printf("projeto do bem!\n");
printf("Agora se ja fizer parte da nossa familia digite 2 para fazer seu\n");
printf("login!\n");
scanf("%i", &resp);
switch(resp) {
case 1:
system ("cls");
Cadastro(Nm, Us, Mail, Sx, Idad, C_P_F, Sen);
break;
case 2:
system ("cls");
Login(Usie, Snh);
break;
}
return 0;
}
There are still opcoes of improvements, as you dynamically allocate the memory of the variables and create a while for the opco menus, but focusing on the purpose of your topic, the above information will solve your problem. Abrasive