Thread: Password program issues

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    2

    Password program issues

    I am having trouble with this project I'm doing.

    Here is the description:

    Some Websites impose certain rules for passwords. Write a program and method that checks whether a string is a valid password. Suppose the password rule is as follows:

    • A password must have at least eight characters.
    A password consists of only letters and digits.
    • A password must contain at least two digits.


    Write a program that prompts the user to enter a password and displays "valid password" if the rule is followed or "invalid password" otherwise. Use the password 'wewew43x' as a test.


    Here is my failing code:

    Code:
    int main()
    {
        char pw[20],p;
        int i=0;
        printf("Enter the password:");
        while((p=getch())!= 13)
        {
             pw[i++] = p;
             printf("*");
        }
    
        pw[i] = '\0';
        printf("\nYou have entered: %s",pw);
        
        if (i <8 )/*If the password has less than 8 characters it is INVALID!*/
           {
           printf("\nInvalid password. Passwords must have at least 8 characters.\n");
            }
        if (!( isalnum(pw[i])) )/* if pw contains special chars it is INVALID*/
            {
            printf("\nInvalid password. Passwords cannot contain special characters.\n");
            }
        if ( isdigit(pw[i]) < 2 ) /*if pw contains less than 2 digits it is INVALID*/
            {
            printf("\nInvalid password. Passwords must contain at least 2 digits.\n");
            }
        else 
            printf("\nValid password. All conditions have been met.\n");
            
      
        return 0;
    }
    My problem is that I'm not thoroughly understanding how to get the functions isalnum & isdigit to work with my code. When I type the password that I want to test with, it just says its invalid, when it should be valid. What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    You're using the functions correctly per se, but the mistake you're making is that you need to loop through the entered password and keep a count of how many digits you have and whether each character is alphanumeric or not.

    If you encounter any non-alphanumeric characters then you can abort the loop straight away and report that the password is invalid.

    If you get to the end of the loop, then check the number of digits. If it's less than 2, then again report invalid password.

    What you're actually doing in lines 19 and 23 is testing the first character beyond the entered password only - not a character you want to test, and you're not checking any of the entered characters at all. See if you can work out where your loop needs to go.
    I think you can put a signature here.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    As Driver said, you're not looking at the right indices in your checking code. This can easily be seen by doing the following exercise: grab a pen and paper, and go through the program "by hand", keeping track of the value of each variable. If you do this, you will clearly see where you are going wrong. Such analysis will help you go a long way in troubleshooting basic code like this.

    It should also be obvious you don't have all the variables you need. For instance, "A password must contain at least two digits" ... this implies that you need to count the number of digits, but you don't have any variables to keep track of this.

    You will not have a lot of luck just trying to write the code all at once, without planning it. Spend a little time planning the program out (again, by hand on paper) before starting to write code. Then break the requirements up into separate "pieces" and work on one at a time. Build the code gradually, testing as you go.

    A development process

    Also, you should be using character literals instead of magic numbers; for instance, '\n' instead of 13.

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    2
    Okay thanks. Good to know I was on the right track before. I tried making a loop before, but it didn't work, maybe I just placed it in the wrong spot out of frustration, or had an argument that didn't actually do anything. I'm glad I am at least using the functions correctly though

    Also, when I tried using a '\n', it would just keep typing forever and ever. I'm sure it's because I just need to rework some things.

    I have my class later today, so I'll come back and try to see if I can fix it. If not I will reply here again. Thanks for the advice.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. password program
    By narendrav in forum C Programming
    Replies: 5
    Last Post: 02-08-2012, 05:59 PM
  2. Password Program
    By lukesowersby in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 11:36 AM
  3. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  4. I need help with a password program
    By italiano40 in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2006, 10:27 PM
  5. password program
    By Draco in forum C Programming
    Replies: 9
    Last Post: 01-19-2003, 06:05 AM