function problem in password generator
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: function problem in password generator

  1. #1

    [solved]function problem in password generator

    hi guys,
    i've written a password generator and would like to make a function of it (for future use), but when i use the function i get into an unending loop, here's the code:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void generate_password(int);
    
    int main(void)
    {
        int password_length;
        printf("\nThis is a password generator.");
        printf("\nEnter the length of the password: ");
        scanf("%d",password_length);
        printf("\nThe generated password is: ");
        
        generate_password(password_length);
        return 0;
    }
    
    void generate_password(length)
    {
        int length;
        srand(time(NULL));    
        while (length>0)
        {
            
            int cha;
            cha=rand()%128;
            if((cha>=48 && cha<=57) || (cha>=65 && cha<=90) || (cha>=97 && cha<=122))
            {
               printf("%c",cha);
               length--;
            }
        }
    }
    problem: when the above code is executed (after compling of course), it gets in a unending loop...

    BUT, with the next code the program works fine:
    Code:
    #include <stdio.h> 
    #include <stdlib.h>  
    #include <time.h>  
    
    int main(void)
    {
      srand(time(NULL));
      int cha;
      int length;
      printf("This is a password generator.");
      printf("\nEnter the length of your password: ");
      scanf("%d",&length);
      printf("\nYour generated password is: ");
      while (length>0)
      {
        cha=rand()%128;
        if((cha>=48 && cha<=57) || (cha>=65 && cha<=90) || (cha>=97 && cha<=122))
        {
            printf("%c",cha);
            length--;
        }
      }
      char ch;
      printf ("\nPress any key to continue");
      ch=getch();
      return 0;
    }
    what did i do wrong by changing the above code to a function?

  2. #2
    King Tutorial-ankhamun
    Join Date
    Jul 2004
    Posts
    897
    Iím no C expert, but shouldnít the top line of the function be:

    Code:
    void generate_password(int length)
    And without

    Code:
    int length;
    My guess is that length is somehow being set to some unholy huge number. Iím messing with it and Iíll let you know what I find.

  3. #3
    i'll give it a try, and thanks for the help.....

    [edit]
    gee, i feel stupid! i just made a very stupid mistake:
    Code:
    scanf("%d",password_length);
    should be :
    Code:
    scanf("%d",&password_length):
    well, thanks for the reply anyway [/edit]

  4. #4
    King Tutorial-ankhamun
    Join Date
    Jul 2004
    Posts
    897
    Ok, the following works for me in GCC under Cygwin and also in Dev-C++:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void generate_password(int);
    
    int main(void)
    {
        
        int password_length;
        printf("\nThis is a password generator.");
        printf("\nEnter the length of the password: ");
        scanf("%d",&password_length);
        printf("\nThe generated password is: ");
       
        generate_password(password_length);
        return 0;
    }
    
    void generate_password(int length)
    {
        srand(time(NULL));
       
        while (length>0)
        {
            
            int cha;
            cha=rand()%128;
            if((cha>=48 && cha<=57) || (cha>=65 && cha<=90) || (cha>=97 && cha<=122))
            {
               printf("%c",cha);
               length--;
            }
        }
    }
    Enjoy

    edit: notice the pass by address in the scanf statement

  5. #5
    hahaha, you figured it out at about the same time as i did, but thanks for your help anyways

  6. #6
    Senior Member
    Join Date
    Jul 2003
    Posts
    634
    those stupid "&", they catch me out all the time and its the first thing i look for now,

    i2c

  7. #7
    i know what you mean

  8. #8
    very nice but these does not support different finds of passwords!!

    like :

    digits only
    letters only
    capitalized letters only
    special characters

    of a combination of all !!

  9. #9
    very nice website lepricaun

    i found some cool stuff there

    check mine >>

  10. #10
    King Tutorial-ankhamun
    Join Date
    Jul 2004
    Posts
    897
    It does i you modify it. Now stop bringing up old threads unless you have something useful to add (like the code to do what you want).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •