Hello there people. I am pretty new in C and programming in general. I was trying to make a password checking programme and I seem to be stuck.

int main (void)
{char ch;
while ((ch=getchar()) != EOF){

This is the beginning of my code and whenever I try to type in a password, ch just gets each character one by one. For example, when I type in 348%tdwe, it says
3 is a valid password
4 is a valid password
e is a valid password.
I am not sure if I am saying this correctly (although I sure do hope so) and I would like to ask you not to give me too complex answers because (again) I am just a month in..
Thank you in advance

Edited by kostaskol

5 Years
Discussion Span
Last Post by kostaskol

You need to validate the password completely, then display the output:

#include <stdio.h>

int main(void)
    const char *pass = "letmein";
    int ch; // int, not char!

    while ((ch = getchar()) != '\n') {
        if (ch != *pass++)

    if (*pass)
        puts("Invalid password");

Note that your code contains a bug as well. ch should be declared as an int to ensure that EOF (a value guaranteed to be outside the range of valid characters) is properly handled.


ooh boy.. thank you so much mate. Really appreciate it..

Edited by kostaskol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.