[Solved] C Programming- Scanf not working. Tried adding space, doesnt work [closed]


Your problems stem from this statement:

scanf("%c",&user);

The %c conversion specifier only reads a single character from the input stream, not a string. If you entered a username like jbode, only the 'j' character is read from the input stream – the remaining characters will be read by the next scanf (or other input) call.

Unfortunately, that next scanf call is expecting a sequence of decimal digits due to the %d conversion specifier; 'b' is not a decimal digit, so you get a matching failureoption is not updated, 'b' is not removed from the input stream, and scanf returns a 0 to indicate that no successful conversion occurred.

The right answer is to not use scanf to read the username at all, but to use fgets instead:

if ( !fgets( user, sizeof user, stdin ) )
{
  // EOF or error detected on input, handle as appropriate
}

checkPass();
...

If you really want to use scanf, though, the right approach would be:

if ( scanf( "%49s", user ) != 1 ) // no & operator on user
{
  // EOF or error detected on input, handle as appropriate
}

checkPass();
...

You do not need to use the & operator when reading a string into an array of char. The expression user will implicitly be converted (“decay”) from type char [50] to char *, and the resulting pointer value will be the address of the first element of the array.

solved C Programming- Scanf not working. Tried adding space, doesnt work [closed]