Problems with the <string. h> library

Asked

Viewed 56 times

0

Good evening, everyone!

I am developing a work of college using functions, structs and files, and I am having problems exactly with the library <string.h>.

Basically, the user registers and their data is sent to a file BancodeUsuarios.txt, that in function Cadastrar, already in office Login was to run the file and check using the strcmp if the usuário and senha are equal to 0, but at the time of rotating he shouts that there is some error in the use of strcmp and in the library, more specifically the:

[Warning] passing argument 2 of 'strcmp' makes pointer from integer without a cast
[Note] expected 'const char *' but argument is of type 'int'

That particular second opens that page: The file opened when I click on the error

Can you help me? I have no idea what my mistake is with this library. I’ll put down the code, I’d also like to know if I’m on the right track.

    #include <stdio.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: ");
        fflush(stdin);
        gets(Nome);
    
    
        printf("Email: ");
        gets(Email);
    
    
        printf("Idade: ");
        scanf("%i", &Idade);
    
        printf("Sexo: ");
        fflush(stdin);
        gets(Sexo);
    
       printf("CPF: ");
       scanf("%i", &CPF);
    
       printf("Nome de usuario: ");
       fflush(stdin);
       gets(User);
    
       printf("Senha: ");
       scanf("%i", &Senha);
    
    
    //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, sesim;
    
        printf("------------Login------------\n");
        printf("Usuario: ");
        fflush(stdin);
        gets(UserVrfc);
    
    
        printf("Senha: ");
        scanf("%i", &SenhaVrfc);
    
        FILE *buser;
        buser = fopen("BancodeUsuarios.txt","r");
    
        //Verifica o nome de usuário
    
        aux1 = 0;
    
        while(fscanf(buser,"%s",r) !=EOF){
           sesim = strcmp(r,UserVrfc);
           if(sesim == 0){
                aux1 = 1;
                break;
           }
        }
    
        fclose(buser);
    
        //Verifica a senha
    
        buser = fopen("BancodeUsuarios.txt","r");
    
        aux2 = 0;
    
        while(fscanf(buser,"%s",r) !=EOF){
           sesim = strcmp(r,SenhaVrfc);
           if(sesim == 0){
                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("cadastro!\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;
    
    }
  • SenhaVrfc is a int, it makes no sense to use the function strcmp to compare with the string r.

1 answer

1

Come on, buddy. I’m in college, too, and I understand your euphoria, but a tip from someone who’s already been hit in life: be specific. Blaming the <string. h> library for the error in your code can cost you a place in the sun here in this great community. Returning to your question, I will quote a few points:

  1. Immediately forget the gets() command. It is discontinued in c and has already been replaced by fgets(). A simpler alternative to fgets is to use a scanf() with a getchar() afterwards, so that the enter (the famous line break) does not interfere with 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. */
    
  2. I tried to understand why I cleaned the buffer at various times of the code with the command "fflush(stdin)", but I realized that has no sense at all. Please remove all from your code immediately.

  3. Your read and compare user and password command is somewhat fragile. The error quoted by Voce 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 pointer and Senhavrfc is an integer, i.e., it is not possible to compare a character with an integer. optimizing the two whiles of user comparison and password, 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;
        }
    }
  1. Your code, with the modifications I’ve proposed, will look 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 options for improvements, such as dynamically allocating the memory of variables and creating a while for options menus, but focusing on the purpose of your topic, the above information will solve your problem. Hug

  • It worked perfectly! Thank you so much for the help and the tips, I didn’t imagine that the gets() was already in disuse, or I didn’t know the getchar() function, I still have a lot to learn!

Browser other questions tagged

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