[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?