Member Avatar for kingben
#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 ......

>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 .....
Well, you could for example at the start of the program, read out all the previously generated passwords from the file, and store them into a vector.
When generating a new password, you should loop through each element of the vector (containing all the previously generated passwords), to check whether the newly generated password wasn't already generated before.
If so, then you generate a new password, and repeat the above process, until you have a new 'unique' password.
When you've obtained such a new 'unique' password, you can write it to the file.

Note:: When your program doesn't directly exit after generating passwords, and for example asks the user to generate a new one, then be sure to every time add the newly generated pass to the vector, to avoid to always have to reread the the entire passwords file's contents.

Edit:: Maybe a tip to make the generated passwords more random:
You could maybe let the user enter a blurb of text on his keyboard (no matter what), and let an algorithm operate on this "blurb of text".

Edit:: Your code is using a lot of deprecated header files, maybe consider migrating your code to standard C++ ?
Here's a link to a nice guide: http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html

Edit:: Deprecated headers in your code:

#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>

using the new-style headers, your code could be rewritten like:

#include<iostream>
#include<[B]c[/B]stdlib>
#include<[B]c[/B]time>
#include<fstream>
#include<[B]c[/B]string>
#include<conio.h>

BTW, or I overlooked it, but I can't see any place in your code, where you're using functions from the unportable conio library.
If you aren't doing so, or if you don't know what conio is/does, then you should just remove that include directive.

Edit:: Your code also contains the very evil: [B]void main()[/B] , get rid of it, it's against the C++ standards, instead you must replace it by: [B]int main()[/B] (in that guide ^^ you'll find the resources needed to turn your code into standard && decent C++ code)

Edit:: Look at this, another example of non-decent C++ code:

#define MIN_PASS_LENGTH 6
#define MAX_PASS_LENGTH 12
#define NUMBER_OF_PASSWORDS 3

in decent C++ code, the const keyword is used for declaring constants within your code:

const int MIN_PASS_LENGTH = 6
const int MAX_PASS_LENGTH = 12
const int NUMBER_OF_PASSWORDS = 3

Edit:: Another thing that is annoying me is that you included a lot of useless comments into your code:

[B]^^ I stripped out all the code above this snippet ^^:[/B]

/*SWITCH:*/ [B](1)[/B]	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 [B](2)[/B]

	} //end for [B](3)[/B]
	temp--;
	file_op<<endl;
	}//end while
	file_op.close();
} //end main [B](4)[/B]

(1): [B]/*SWITCH:*/[/B] : everyone can see that it's a switch.
(2), (3), (4): everyone can see where the for-loop ends, where the closing bracket of the switch is, or where the end of the main function is, these are thus pretty much useless as comments.

commented: awesome!!! +1
commented: Nicely done +36
Member Avatar for kingben

Hey thanks a lot for all your comments! Really helpful.
one thing that i would like to mention

Edit:: Another thing that is annoying me is that you included a lot of useless comments into your code:

actually i commented out the statements which i was previously using in the program that I made. e.g. i didn't used file handling and didn't bothered about l_count and i_count variables. After achieving out my aim at lower level, i commented out the fields and modified the code .....
while posting to the forum i forgot to remove those comments ....

sorry for that :icon_neutral:

thanks a lot again for all what you provided!

Hey thanks a lot for all your comments! Really helpful.
one thing that i would like to mention

Edit:: Another thing that is annoying me is that you included a lot of useless comments into your code:

actually i commented out the statements which i was previously using in the program that I made.

Yes, yes, I know.
I wasn't complaining about the the out-commented statements, I only wanted to notify you that comments in your code like: // end of main are just unnecessary clutter in your code, clutter which serves no useful purpose :)
But of course, when your code reaches its final stage, I'd recommend you to remove the unneeded out-commented statements from your code, but while developing, out-commenting a/multiple statement(s) is always useful.

Edit:: I exaggerated maybe a bit when I said: "a lot of useless comments" :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.