Compare two simple strings in C, input with scanf();

Asked

Viewed 752 times

0

what I intend to do is quite simple. I make the program ask for a password, the program compares, and if the password is valid, the program proceeds.

An example:

char pass[5];

printf("insira a senha: ");

while(pass!="asdfg") { //enquanto a senha for diferente/inválida
    scanf("%s",&pass); //entrada da senha
};

When I try to execute this code, the program simply does not accept the password asdfg and keeps asking for the password.

  • In this little piece of code of yours, the error lies not only in comparison, but in other places as well. Too bad you can’t show it, since the question was closed.

1 answer

0

When picking up a string you don’t need to use &, J that string name is already a memory address.

char pass[5];

printf("insira a senha: ");

while(!strcmp(pass,"asdfg")) { //enquanto a senha for diferente/inválida
    scanf("%s", pass); //entrada da senha
};
  • The problem is wider than that

  • @Amadeus because?

  • I realized I made the change, thanks @Amadeus

  • In C, it does not compare strings to !=. Another problem, scanf is prone to buffer overflow and finally, %s does not capture words with space

Browser other questions tagged

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