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:
problem: when the above code is executed (after compling of course), it gets in a unending loop...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--; } } }
BUT, with the next code the program works fine:
what did i do wrong by changing the above code to a function?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; }




Reply With Quote