Password help

This is a discussion on Password help within the C Programming forums, part of the General Programming Boards category; Ok, I was trying to make a password program just to test it out for practice ( I am new ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103

    Password help

    Ok, I was trying to make a password program just to test it out for practice ( I am new to C). however, there are no grammar errors in this code, it just doesn't flow right or something. So if someone could tell me why it doesn't work right o give a sugestion of another way of doing it.. that would be helpful... thanks..


    ***Also, ignore the part where I print the string length, that was just for testing purposes***

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        int z;
        int q;
        int ctr = 0;
      
      char password[] = "Password";
      char entry[20];
    
      
      printf("To get into the program, you must enter the correct password.\n");
      printf("Type in your password(upper and lower case matter).\n");
      gets(entry);
      
      i = strlen(password);
      printf(" %d\n", i);
      
      for(z =0; z <= i; z++)
      {
            if(password[ctr] == entry[ctr])
            {
                             q++;
            }
            ctr++;
      }
      if(q == i)
      {
           printf("You are in\n");
      }
      else
      {
          printf("Access denied\n");
      }
      
      
      
      system("PAUSE");	
      return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "gets" appends the newline to the string, so you may want to remove one char from the string (if it's a newline).

    Also, don't use "gets", use "fgets()", as gets is unsafe (if someone enters a string that is longer than 20 characters, your program will most likely crash.

    Is there any reason why you are not just bailing out with "mismatch=1" when the characters in the password and the entry are different, instead of counting the number of matching characters and checking if "all matched". If one or more mismatch, who cares how many they are?

    Also, if someone enters a password longer than "password", you will compare "garbage after the end of "password" - so you may want to stop as soon as you see a difference [If the entry is longer than password, it will mismatch when it reaches the end of "password", as that is the zero-character indicating end of string.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    rob... if you mean to say at the beginning "q = o;"... I tryed it, and it doens't make it work.. and mat.. I don't want to make a file.. and to use fgets... don't you need a file to be read from????

    Well, I tryed your way mat and it worked, but I am not quite sure why yours did and mine didn't, it is pretty much the same thing, maybe it messed up the counters somewhere....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        int z;
        int q = 0;
        int ctr = 0;
      
      char password[] = "Password";
      char entry[20];
    
      
      printf("To get into the program, you must enter the correct password.\n");
      printf("Type in your password(upper and lower case matter).\n");
      gets(entry);
      
      i = strlen(password);
      printf(" &#37;d\n", i);
      
      for(z =0; z <= i; z++)
      {
            if(password[ctr] != entry[ctr])
            {
                             q++;
            }
            ctr++;
      }
      if(q == 0)
      {
           printf("You're in\n");
      }
      else
      {
          printf("Access Denied\n");
      }
      
      
      
      system("PAUSE");	
      return 0;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you need to tell fgets the file-stream to read from, but there is an already existing one for "stdin" that works quite well for this purpose.

    You may just as well do "q = 1; break;" in your if-not-equal statement.

    --
    Mats

  6. #6
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    So your saying this could be an example:

    Code:
    char blah[10000000000];
    
    fprintf(stdin, "blah blah\n");
    
    
    fgets(stdin, blah);
    and that would just use a preexsisting file.... (assuming that I am on the right line and all that jazz)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "fgets(entry, sizeof entry, stdin)" would be better.

    --
    Mats

  8. #8
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    oh ya the file name goes at the end... but same concept..ish... oh well... thanks.. you're always a big help

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    fprintf(stdout, "blah blah\n");
    
    //is the same as
    
    printf("blah blah\n");

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    > "gets" appends the newline to the string, so you may want to remove one char
    > from the string (if it's a newline).

    'fgets()' appends the newline character, not gets(), so your program will fail if you
    use it, because you end up comparing password[8] ('\0') with entry[8] ('\n') - if the
    user entered the correct password. You should remove the trailing newline character,
    as Mats said.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        int i, z, q;
        int ctr = 0;
        char password[] = "Password";
        char entry[20];
    
        printf("To get into the program, you must enter the correct password.\n");
        printf("Type in your password(upper and lower case matter).\n");
    
        /* fgets(entry, sizeof entry, stdin); 
        perhaps you might need this as well if you use fgets
        char *p;
        p =  strchr(entry,'\n');
        P = '\0';
        */
        gets(entry); 
      
        i = strlen(password);
        printf(" &#37;d\n", i);
      
        for(z=0; z <= i; z++)
        {
            if(password[ctr] == entry[ctr])
                q++;
                
            ctr++;
        }
        
        if(q == i)
            printf("You are in\n");
        else
            printf("Access denied\n");
    
        /* use  getchar(): */  
        system("PAUSE");	
        return 0;
    }
    
    /* my output
    To get into the program, you must enter the correct password.
    Type in your password(upper and lower case matter).
    Password
     8
    Access denied
    Press any key to continue . . .
    
    */
    Perhaps you could display the chars enetred with '*' as an improvement to your code.

    ssharish2005

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    why not use strncmp()? Saves a lot of effort.

    Quote Originally Posted by ssharish2005
    char *p;
    p = strchr(entry,'\n');
    P = '\0';
    You should check if p is not null before proceeding, and p != P

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    You should check if p is not null before proceeding, and p != P
    Should have, i dint

    ssharish2005

  14. #14
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Well, I think the person who actually added comments to my code didn't get it to wokr because your missing brackets ({) on the if statement.. so it adds one to q no matter what... so you will never get it correct.. and as for displaying "*" for the type how would you do that if you wanted to still receive the password, but instead of them seeing it, they see the * just like a password in anything else???

  15. #15
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Oh, and also, that isn't the revised code.. this is the revised code that works.....


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        int z;
        int q = 0;
        int ctr = 0;
      
      char password[] = "Password";
      char entry[20];
    
      
      printf("To get into the program, you must enter the correct password.\n");
      printf("Type in your password(upper and lower case matter).\n");
      gets(entry);
      
      i = strlen(password);
      printf(" &#37;d\n", i);
      
      for(z =0; z <= i; z++)
      {
            if(password[ctr] != entry[ctr])
            {
                             q++;
            }
            ctr++;
      }
      if(q == 0)
      {
           printf("You're in\n");
      }
      else
      {
          printf("Access Denied\n");
      }
      
      
      
      system("PAUSE");	
      return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 09:42 PM
  4. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 09:54 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 01:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21