Thread: Program to test a user input vs a saved password

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    Program to test a user input vs a saved password

    Hi guys writing a program for my university course that need to check a user input password against one saved in the program. So far it always returns incorrect and I'm not sure why.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char Password[4], UserInput[4];
        Password[4] = "fred";
    
        printf("Please enter your password: ");
        scanf("%s", UserInput);
        if (Password == UserInput)
        {
            printf("correct");
        }
        else
        {
            printf("incorrect");
        }
    
    getchar();
    return 0;
    
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. You need to use strcmp function to compare such strings. Using == to compare arrays actually compares the addresses of the two arrays on the stack. I would hope you understand that two discrete objects on the stack should never have the same address and therefore the comparison would never return true.
    2. Arrays use a zero based index, an array of 4 characters has valid indexes from 0 up through and including 3. Password[4] is not only outside of this range of valid indexes, you are also attempting to assign a string literal to a single character which does not work. You can initialize an array upon instantiation of the array or you can strcpy the string literal to the array to initialize after the point of the array's instantiation.
    3. A string is an array of null-terminated characters, as such, using the strcmp function requires that the arguments passed into it are valid strings. "fred" requires at least a 5 character array to properly store it. Your UserInput array would also need to be a minimum of 5 characters and not 4.
    4. You would likely need another getchar at the end of your program to actually halt the program. After entering the password and pressing "enter" the newline character would remain in the input buffer until the program reached the getchar function. At this point the newline would be consumed and the program would exit without waiting for more input. A second getchar would block the program and wait for the user to press "enter" as I believe you are attempting.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 08:04 AM
  2. Simple Program For Accepting User Name & Password.
    By Zaheer Attar in forum C Programming
    Replies: 2
    Last Post: 01-10-2012, 04:56 AM
  3. Replies: 2
    Last Post: 07-24-2008, 06:05 AM
  4. Deleting IE saved user names and passwords
    By patrick22 in forum Windows Programming
    Replies: 3
    Last Post: 03-23-2008, 04:38 PM
  5. how to test if user input is an int
    By sizzle_chest in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2001, 05:58 PM