Thread: Password program problem.

  1. #1
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Exclamation Password program problem.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int CheckPass(char *user_id, char *password);
    void AccessGranted();

    void main()
    {
    char user_id[20], password[20];
    int flag, change=3;
    int count=3;

    do
    {
    /*Show the number of attempt(s) left*/
    printf("Number of attempt(s) left: %d\n", count);

    /*Gets user ID from user*/
    fflush(stdin);
    printf("Enter your user ID > ");
    scanf("%[^\n]", user_id);

    /*Gets password from user*/
    fflush(stdin);
    printf("Enter your password > ");
    scanf("%[^\n]", password);

    system("cls");

    flag=CheckPass(user_id, password);

    if(flag==1)
    {
    printf("\nACCESS GRANTED\n\n");
    AccessGranted();
    }
    else
    {
    printf("\nACCESS DENIED\nUser ID and password does not match\n\n");
    count--;
    if(count==0)
    printf("\nYou have no more attempt(s) left.\nProgram terminated\n\n");
    }
    }while(count>0);
    }

    int CheckPass(char *user_id, char *password)
    {
    FILE *fpPass;

    char fuser_id[20], fpassword[20];
    int i, temp;
    int found=0;

    if(!(fpPass=fopen("Password.txt", "r")))
    {
    printf("Error: Unable to open password file.\nSorry for the inconvenience\n\n");
    exit(0);
    }

    while(fscanf(fpPass, "%[^\n]\n%[^\n]\n", fuser_id, fpassword)!=EOF && !found)
    {
    /*Check for valid user ID*/
    if(strlen(fuser_id)==strlen(user_id))
    for(i=0, temp=strlen(fuser_id); i<temp; i++)
    {
    found=1;
    if(*(user_id+i)!=fuser_id[i])
    found=0;
    }
    }

    fclose(fpPass);

    if(!found)
    return 2;
    else
    {
    /*Check for valid password*/
    if(strlen(password)!=strlen(fpassword))
    return 0;
    else
    {
    for(i=0, temp=strlen(fpassword); i<temp; i++)
    if(*(password+i)!=fpassword[i]-2)
    return 0;
    }
    }

    return 1;
    }

    void AccessGranted()
    {
    printf("Thank you for using this system.\n\n");
    exit(0);
    }

    The above program read input from user (ID and password).
    The encrypted password is stored in Password.txt. The contain of Password.txt:
    joe
    dcvocp
    joe1
    dcvocp3

    The problem is, when user key in joe, the correct password is batman1, not batman. Please help!!!
    It's unfulfilled dreams that keep you alive.

    //netboy

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You should be checking the user ID and password in the same loop. Presently your code is reading a user ID and password from the file, checking the user ID and then reading another record from the file before checking the password (overwriting the first password).
    zen

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    26

    Exclamation

    I didn't read past void main()

    1.) Why does AccessGranted() return void? How is the rest of your program to know that access was granted?

    2.) You should be slapped for using void main() ....
    one fish two fish
    red fish blue fish

  4. #4
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158
    I got it. Thanks for helping!!!
    Sad to say that I'm still a beginner...Please don't be mad!!!
    :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Password Program
    By lukesowersby in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 11:36 AM
  2. First password program
    By MaaaTtY in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2009, 12:29 PM
  3. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM