Converting int to string

Asked

Viewed 9,962 times

0

I made a program that simulates a login and when converting the password that is int to string using sprintf it from segimentation falt, I tried to use itoa but it from the definition error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   char login[6]={0};

   int passwd;
   char senha[6]={0};

   char logins[6]="Teste";
   char senhas[6]="12345";

  do{
     printf("\n\tDigite o Login: ");
     scanf("%s",login);

     if((strcmp(login, logins) == 0))break;
     else
     printf("\tUsuario: %s Inválido..!!\n", login);

     }while((strcmp(login, logins) != 0));

  do{
     printf("\n\tDigite sua senha: ");
     scanf("%d",passwd);

     sprintf(senha,"%s",passwd);

     if((strcmp(senha, senhas) == 0))
     printf("\n\tUsuario e Senha Válidos\n\tAcesso autorizado..!!!\n\n");
       else
     printf("\tSenha: %s Inválida..!!\n", senha);

    }while((strcmp(senha, senhas) != 0));

   return 0;
}
  • If the password is a int because it needs to be converted to string ? If it’s supposed to be a string why not read string directly ?

  • pq this is another way to use comparison to log in, and then need to check if the password is int convert to char* compare and log in if it is equal but not able to convert this password to string...

1 answer

1


 printf(senha,"%s",passwd);

"%s" should only be used if passwd were an array of characters(char[]) as an integer use "%d"

However use the snprintf function, it avoids Segmentation fault, that 6 there is the size of your array, given the size of your array, the function will never write more than the allowed.

snprintf(senha, 6, "%d", passwd);

ITOA (Since no solution above worked)

Since your compiler lib does not have the function to convert int to char*, let’s create a!!!:

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *  itoa ( int value, char * str )
{
    char temp;
    int i =0;
    while (value > 0) {
        int digito = value % 10;

        str[i] = digito + '0';
        value /= 10;
        i++;

    }
   i = 0;
   int j = strlen(str) - 1;

   while (i < j) {
      temp = str[i];
      str[i] = str[j];
      str[j] = temp;
      i++;
      j--;
   }
    return str;


}

int main()
{
   char login[6]={0};

   int passwd;
   char senha[6]={0};

   char logins[6]="Teste";
   char senhas[6]="12345";



  do{
     printf("\n\tDigite o Login: ");
     scanf("%s",login);

     if((strcmp(login, logins) == 0))break;
     else
     printf("\tUsuario: %s Inválido..!!\n", login);

     }while((strcmp(login, logins) != 0));

  do{
     printf("\n\tDigite sua senha: ");
     scanf("%d",&passwd);

     //sprintf(senha,"%s",passwd);
     //snprintf(senha, 6, "%d", passwd);
      itoa(passwd,senha);

     if((strcmp(senha, senhas) == 0))
     printf("\n\tUsuario e Senha Válidos\n\tAcesso autorizado..!!!\n\n");
       else
     printf("\tSenha: %s Inválida..!!\n", senha);

    }while((strcmp(senha, senhas) != 0));

   return 0;
}

Tested and working out. In the scanf you have to pass the variable by reference getting like this:

scanf("%d",&passwd);

sample of the above online code execution https://onlinegdb.com/SkcoQAIbf

  • then I tried to use also this snprintf and sprintf both with %d and %s and still resulted in segmentation failures, I even tried using the Std::to_string function and copying it to the char array and even then gave segmentation failure, however if I declare the passwd as a pointer it does not from the segmentation fault but at the time of entering the password it returns me value 90 or it makes a sum of the password and multiplies by the size of the array value... I do not know but what to do to convert int to string the function itoa no longer exists in the gcc-compilerg++ 7.2.0...

  • I think agr goes friend, this time without using sprintf or some function like.

  • So @Vitorrisk what if I tell you that it wasn’t still segmentation failure? I’ve seen this function researching about the itoa and I tried to adapt in my code as much as the other version of the itoa where you enter with the length to tell the size of the vector and even so gave segmentation failure... I’ve used both the: itoa(int, char*) and the itoa(char*, const char*, int); and even so gave segmentation failure, ta dificil entender essa bagaça...

  • I executed on the site https://onlinegdb.com/ the following code below with the suggested edits and even then from Segmentation falt. https://pastebin.com/rydbmF9f

  • now ta ok https://onlinegdb.com/B1dz7AIbM

  • the only error of your code was: scanf("%d", passwd) notice that passwd is being passed as value and not as reference, I made storm in water cup

  • That’s all Raccoon was? what cool I learned that reference was not nescessario to do mainly when it was working with string... Caraca was worth now yes rode here and ta funfando same...

Show 2 more comments

Browser other questions tagged

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