Ask coding questions

← Back to all posts
Check password errors
5S-1919

I have written a program as shown, the program was designed for Checking the password and its works fine if i am entering integer. But when i enter a English letter it keep repeating Please enter, how can i avoid this?
I am relatively new to C program so explains could be really helpful.

Comments
hotnewtop
SixBeeps

When you specify %d in the scanf function, you're telling the computer to read a decimal number. Letters aren't numbers, so you need to change your typing.

First thing, your password variable is of type int. Change this to char[].
Second thing, change the type from %d to %s.

Read more here: https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm

5S-1919

@SixBeeps Thx a lot :D

5S-1919

There a error here and it didn't show my program(in my computer now), so i am just gonna paste the program here
//This program is for checking the password,the password has been set to 13579 and it works fine except when I type a English letter(y for examples), it goes to a loop where it keep repeating Enter password.

#include <stdio.h>

int main()
{
int password='0';
/ The value of the variable password is checked
whether it equals 13579. If not, then the body
of the while loop continus execution.
/
while(password != 13579) {
printf("Enter password: ");
scanf("%d", &password);
}

}