#define MIN_PASS_LENGTH 6
#define MAX_PASS_LENGTH 12
#define NUMBER_OF_PASSWORDS 3
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
//int i; //letters
//int j; //numerals
char rand_small_letter() {
int nHigh = 122;
int nLow = 97;
int small_letter = ((rand()%(nHigh - nLow + 1)) + nLow); //values from 97 to 122
return (char)(small_letter);
}
char rand_big_letter() {
int nHigh = 90;
int nLow = 65;
int big_letter = ((rand()%(nHigh - nLow + 1)) + nLow); //values from 65 to 90
return (char)(big_letter);
}
/*
int rand_number() {
int nHigh = 9;
int nLow = 0;
return ((rand()%(nHigh - nLow + 1)) + nLow); //values from 0 to 9
}
int rand_number_switch() {
int nHigh = 3;
int nLow = 1;
return ((rand()%(nHigh - nLow + 1)) + nLow); //values from 1 to 3
}
*/
int rand_number(int nLow, int nHigh) {
return ((rand()%(nHigh - nLow + 1)) + nLow);
}
/*
void ask_parameters() {
START:
cout<<"How many letters do you want in the password?: ";
cin>>i;
cout<<"How many numerals do you want in the password?: ";
cin>>j;
if((i+j)<MIN_PASS_LENGTH) {
clrscr();
cout<<"Minimum Password length is "<<MIN_PASS_LENGTH<<endl;
goto START;
}
}
*/
void main() {
fstream file_op("passwords.txt",ios::out|ios::in); //open in write,read mode
file_op.seekp(0,ios::end);
//ask_parameters();
srand((unsigned int)time(0));
//int l_count = 0;
//int i_count = 0;
int temp = NUMBER_OF_PASSWORDS;
while(temp) {
for(int k=0;k<rand_number(MIN_PASS_LENGTH,MAX_PASS_LENGTH);k++) {
/*SWITCH:*/ switch(rand_number(1,3)) {
/*SLETTER:*/ case 1:
//if(l_count >= i)
// goto NUMBER;
file_op<<rand_small_letter();
//l_count++;
break;
/*BLETTER:*/ case 2:
//if(l_count >= i)
// goto NUMBER;
file_op<<rand_big_letter();
//l_count++;
break;
/*NUMBER:*/ case 3:
//if(i_count >= j)
// goto SWITCH;
file_op<<rand_number(0,9);
//i_count++;
} //end switch
} //end for
temp--;
file_op<<endl;
}//end while
file_op.close();
} //end main
I am trying to generate random passwords.
One doubt::
I also want to read from the passwords file and check if the password has been already generated...... If yes, then generate a new password .....
Where should i put the check code for that?? Since am directly writing the password to the file, how do i keep a check on it??
one thing that just came to my mind
} //end for
temp--;
file_op<<endl;
[b]check_string()[/b]
}//end while
file_op.close();
} //end main
the check_string() function reads the newly entered string in the text file and then compares it with all the previous values in the text file ...... If a match is found temp is increased by 1 and the string being found "guilty" is deleted .....
how's it???? ...... but it's seeming to be inefficient ......