|
-
August 7th, 2004, 06:16 PM
#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?
-
August 7th, 2004, 09:47 PM
#2
I’m no C expert, but shouldn’t the top line of the function be:
Code:
void generate_password(int length)
And without
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.
-
August 7th, 2004, 10:17 PM
#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]
-
August 7th, 2004, 10:22 PM
#4
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
-
August 8th, 2004, 10:10 AM
#5
hahaha, you figured it out at about the same time as i did, but thanks for your help anyways
-
August 8th, 2004, 04:22 PM
#6
those stupid "&", they catch me out all the time and its the first thing i look for now,
i2c
-
August 8th, 2004, 04:52 PM
#7
i know what you mean
-
September 23rd, 2004, 02:58 PM
#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 !!
-
September 23rd, 2004, 03:00 PM
#9
very nice website lepricaun
i found some cool stuff there
check mine >>
-
September 23rd, 2004, 03:01 PM
#10
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
-
Forum Rules
|
|