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

Recommended Answers

All 2 Replies

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++)
            break;
    }

    if (*pass)
        puts("Invalid password");
    else
        puts("Welcome!");
}

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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.