Thread: Password Assignment

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    17

    Password Assignment

    I'm supposed to make a program that lets you type in a password that has to be atleast 8 characters, with at least 2 digits.

    Not sure if this is right, but I haven't been able to test it because i keep getting a syntax error before both of the "else" part of the program.




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main()
    {
        char x[30];
        int i=0; 
        int numdig=0, letters=0, ch;
        
        printf("Enter Password:");
        scanf("%s", x);
        x[strlen(x)]= '\n';
        
        while(gets(x)!= NULL)
        {
                    
         
          if(isdigit(x[i]))
          numdig++;
          i++;
          
          else if((islower(x[i])) || (isupper(x[i])))     
           letters++;
           i++;             
               
          else
               break;         
                        
                        }
                        
                        
            if ((strlen(x)>= 8) &&(numdig>=2) )
            printf("Valid Password\n");    
            
            else
            printf("Invalid Password");
            
        while ((ch = getchar()) != '\n' && ch != EOF);
        getchar();
        return 0;
        
        }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well lets clean that code up and look at it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main()
    {
        char x[30];
        int i=0; 
        int numdig=0, letters=0, ch;
        
        printf("Enter Password:");
        scanf("%s", x);
        x[strlen(x)]= '\n'; //for a c string this should be '\0'
    
        while(gets(x)!= NULL) //what are you doing here? Maybe a simple for loop would suffice (for int i=0;i<strlen(x);i++)
        {
    		if(isdigit(x[i])){ //<------you forgot all your brackets
    			numdig++;
    			i++;
    		}
    		else if((islower(x[i])) || (isupper(x[i]))){ //isalpha() would work here    
    			letters++;
    			i++;             
    		}  
    		else
               break;         
         }
         if ((strlen(x)>= 8) &&(numdig>=2) )
    	printf("Valid Password\n");    
         else
    	printf("Invalid Password");
            
        while ((ch = getchar()) != '\n' && ch != EOF);
        getchar();
        return 0;
        
    }
    Last edited by AndrewHunter; 07-14-2011 at 12:37 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Thanks again ! I thought about the while loop and I just forgot about the for loop.

    And i didn't have to add the i++ in all my other if -if else statements.

    Off to do my last one!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    How would you make it read the password without displaying what you typed on the screen?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There's an example of that here, though it uses non-standard functions.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    The only way to do that is with non-standard functions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  3. Using scp without password
    By nitinmhetre in forum Networking/Device Communication
    Replies: 2
    Last Post: 12-20-2006, 05:45 AM
  4. Password
    By KJ_Magic in forum C++ Programming
    Replies: 15
    Last Post: 04-19-2003, 07:29 PM
  5. password
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-15-2002, 02:38 PM