Thread: C login program help

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    1

    C login program help

    Hi guys, I'm new here and I thought of seeking some help regarding my program.

    I made this program where it asks for a username and password for access. The username works well, but whenever I enter the password I programmed, it always says "access denied." Is there anyone who can help me? Here's my code and Thanks in advance!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
    char a,c;
    system("cls");
    printf("Enter your username: ");
    scanf("%s",&a);
    printf("\nEnter your password: ");
    scanf("%s",&c);
    if(c=='ald')
    {
    printf("\nAccess Granted! Welcome %s!",a);
    }
    else if (c=='paul')
    {
    printf("\nAccess Granted! Welcome %s!",a);
    }
    else if (c=='bil')
    {
    printf("\nAccess Granted! Welcome %S!",a);
    }
    else
    printf("\nAccess Denied!");
    getch(); 
    return main();
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You need to learn about C-strings.

    C Strings - Cprogramming.com

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    - What do you think the difference between 'paul' and "paul" is?
    - How many characters do you think c can store?
    - What the hell is getch?

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Tainan, Taiwan, Taiwan
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        char a[20],c[20];
        //system("cls");
        printf("Enter your username: ");
        gets(a);
        printf("\nEnter your password: ");
        gets(c);
        if(strcmp(c, "ald"))
        {
            printf("\nAccess Granted! Welcome %s!",a);
        }
        else if (strcmp(c, "paul"))
        {
            printf("\nAccess Granted! Welcome %s!",a);
        }
        else if (strcmp(c, "bil"))
        {
            printf("\nAccess Granted! Welcome %s!",a);
        }
        else
            printf("\nAccess Denied!");
        //getch();
        printf("\n%s", a);
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. Did you test this version? See what happens when you try to enter an invalid password. Then read up on the return value of "strcmp()".

    2. If you're using "strcmp()", you need to include string.h

    3. "gets()" is dangerous - it would be safer to use "fgets()"

    4. You should use more descriptive variable names - 'a' and 'c' don't tell us anything about what those variables are used for.

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Also it is not helpful to post the solution for the person.

    Give a man a fish......

    >What the hell is getch?
    TurboC allows getch with the inclusion of conio.h.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Tainan, Taiwan, Taiwan
    Posts
    10
    i forgot to include string standard library. I give direct answer because he is not doing homework, he is doing example. In this case, can learn by example. why gets is dangerous? fgets is dedicated to FILE string ?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Smells like homework to me, but that doesn't matter. Giving the solution robs the OP learning how to program. In my opinion you learn by doing, not by copying.

    why gets is dangerous?
    Because it doesn't limit the amount of characters it will receive. This can easily lead to buffer overruns.

    fgets is dedicated to FILE string ?
    Yes, so? The console is treated as a FILE in C. This FILE* is named stdin for the input stream and stdout for the output stream.

    Jim

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by eegii_evol View Post
    i forgot to include string standard library. I give direct answer because he is not doing homework, he is doing example. In this case, can learn by example. why gets is dangerous? fgets is dedicated to FILE string ?
    Sir Your Program Also Not Working Proper I Think!!!
    Because, If we Enter a Wrong Password Also it Shows the Output Like "Access Granted! Welcome"
    Did You Notify that Issue

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Tainan, Taiwan, Taiwan
    Posts
    10
    thanks for practical stuff. most of my knowledge are acquired from books. but, soon i will start to work. useful suggestion thanks

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Tainan, Taiwan, Taiwan
    Posts
    10
    I checked before post. it was working. dunno why haha

  12. #12
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by eegii_evol View Post
    I checked before post. it was working. dunno why haha
    yes!Your Correct..It's Working For the Given Values...But It does n't Show the Output Like "Access Denied!" when Entering the Wrong Password

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-08-2009, 12:55 PM
  2. Login program
    By kollurisrinu in forum C Programming
    Replies: 26
    Last Post: 12-15-2007, 02:19 PM
  3. Login program
    By kollurisrinu in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2007, 07:17 AM