12 Replies - 3447 Views - Last Post: 29 December 2009 - 12:14 AM Rate Topic: -----

#1 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Random Password Generator

Posted 26 December 2009 - 11:04 PM

I decided that I would program a little over semester break so when I start my next programming class I won't be too rusty. I wanted to design a program which generates a random password by using the pseudo-random number generator. I want it to be able to create passwords of lowercase letters, lower and uppercase letters, lower and uppercase letters and numbers, and finally lower and uppercase letters, numbers, and symbols.

The program is not complete but I have run into a problem in my function which creates a password of upper and lowercase letters.
As far as I can tell the function works properly until the for loop where the password is assembled from the constituent letters. It handles the different letters correctly until it runs out of one or the other type of of characters being used.

I need some help developing the logic which will fill the password vector correctly.

Here is my source code:

void lowerAndUpperCase(int length)
{//generates a password of lower and upper case letters
 //length is user entered in the getData function

	char upperCase, lowerCase;		//holds the random character of each case
	int numberCaps, numberLower;	  //the total number of upper/lowercase letters to be generated
	int counterUpper, counterLower;   //counter which will count up to the number of upper/lowercase letters
	int randInt;					  //random number to determine whether an upper or lowercase letter is next

	vector <char> password;		   //holds the resulting password

	counterUpper = 0;				 //used in the for loop where the password is assembled
	counterLower = 0;



	srand(time(NULL));				//seeds the pseudo-random generator

	numberCaps = rand() % length + 1; //initial number of capital letters

	//If the number of capital letters is too small, program will calculate a new number
	while(length > 2 && numberCaps < 2)
		numberCaps = rand() % length + 1;

	while(numberCaps >= ((2*length)/3))
		numberCaps = rand() % length + 1;

	while(numberCaps <= (length/3))
		numberCaps = rand() % length + 1;

	numberLower = length - numberCaps;  //number of lowercase letters is totally dependent on numberCaps

	int upperSize; 
	upperSize = numberCaps;
	int *capsArray = new int [upperSize];
	//array to hold the uppercase letters

	int lowerSize; 
	lowerSize = numberLower;
	int *lowerArray = new int [lowerSize];
	//array to hold the lowercase letters

	for(int i=0; i < numberCaps; i++)
	{//generates uppercase letters
		upperCase = rand() % 25 + 65;

		capsArray[i] = upperCase;
	}

	for(int j=0; j <= numberLower; j++)
	{//generates lowercase letters
		lowerCase = rand() % 25 +97;

		lowerArray[j] = lowerCase;
	}

	for(int z=0; z < length; z++)
	{//assembles password vector
	 //ERROR IN HERE SOMEWHERE
	 
		randInt = rand() % 10+1;

		if(randInt <= 5 && counterLower <= numberLower)
			password.push_back (lowerArray[counterLower]);

		else if(randInt >= 6 && counterUpper <= numberCaps )
			password.push_back (capsArray[counterUpper]);

		if(counterUpper > numberCaps)
			password.push_back(lowerArray[counterLower]);

		else if(counterLower > numberLower)
			password.push_back(capsArray[counterUpper]);

		

		counterUpper ++;
		counterLower ++;

	}

	cout<<"Your password is: ";
	
	for(int q=0; q < length; q++)
		cout<<password[q];

	cout<<endl<<endl;


}


Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Random Password Generator

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Random Password Generator

Posted 26 December 2009 - 11:40 PM

random_shuffle() is pretty fun too.
algorithm.h

If you're using visual studio press debug and set some breakpoints, use the watch feature to watch the values of your variables, etc. Pinpoint where it's doing something wrong. Debugging is a fun part of programming.

This post has been edited by taylorc8: 26 December 2009 - 11:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Random Password Generator

Posted 26 December 2009 - 11:48 PM

I'm debugging this thing right now, I do notice your
for(int z=0; z < length; z++)
	{//assembles password vector
	 //ERROR IN HERE SOMEWHERE



the length is a negative number

// the numberLower is used to dynamically allocate an array at the top, shouldn't this be < numberLower ??
	for(int j=0; j <= numberLower; j++)
	{//generates lowercase letters
		lowerCase = rand() % 25 +97;

		lowerArray[j] = lowerCase;
	}



it's accessing out of bounds on your lowerArray, however, I did get this code to work, with a few modifications..
void lowerAndUpperCase(unsigned int length)
{//generates a password of lower and upper case letters
//length is user entered in the getData function

	char upperCase, lowerCase;		//holds the random character of each case
	unsigned int numberCaps, numberLower;	  //the total number of upper/lowercase letters to be generated
	unsigned int counterUpper, counterLower;   //counter which will count up to the number of upper/lowercase letters
	unsigned int randInt;					  //random number to determine whether an upper or lowercase letter is next

	vector <char> password;		   //holds the resulting password

	counterUpper = 0;				 //used in the for loop where the password is assembled
	counterLower = 0;



	srand((int)time(NULL));				//seeds the pseudo-random generator

	numberCaps = rand() % length + 1; //initial number of capital letters

	//If the number of capital letters is too small, program will calculate a new number
	while(length > 2 && numberCaps < 2)
		numberCaps = rand() % length + 1;

	while(numberCaps >= ((2*length)/3))
		numberCaps = rand() % length + 1;

	while(numberCaps <= (length/3))
		numberCaps = rand() % length + 1;

	numberLower = length - numberCaps;  //number of lowercase letters is totally dependent on numberCaps

	int upperSize; 
	upperSize = numberCaps;
	int *capsArray = new int [upperSize];
	//array to hold the uppercase letters

	int lowerSize; 
	lowerSize = numberLower;
	int *lowerArray = new int [lowerSize];
	//array to hold the lowercase letters

	for(unsigned int i=0; i < numberCaps; i++)
	{//generates uppercase letters
		upperCase = rand() % 25 + 65;

		capsArray[i] = upperCase;
	}
	
	// the numberLower is used to dynamically allocate an array at the top, shouldn't this be < numberLower ??
	for(unsigned int j=0; j <= numberLower; j++)
	{//generates lowercase letters
		lowerCase = rand() % 25 +97;

		lowerArray[j] = lowerCase;
	}

	for(unsigned int z=0; z < length; z++)
	{//assembles password vector
	 //ERROR IN HERE SOMEWHERE
	 
		randInt = rand() % 10+1;

		if(randInt <= 5 && counterLower <= numberLower)
			password.push_back (lowerArray[counterLower]);

		else if(randInt >= 6 && counterUpper <= numberCaps )
			password.push_back (capsArray[counterUpper]);

		if(counterUpper > numberCaps)
			password.push_back(lowerArray[counterLower]);

		else if(counterLower > numberLower)
			password.push_back(capsArray[counterUpper]);

		

		counterUpper ++;
		counterLower ++;

	}

	cout<<"Your password is: ";
				delete [] capsArray;
	delete [] lowerArray;
	
	for(unsigned int q=0; q < length; q++)
		cout<<password[q];

	cout<<endl<<endl;


}


A little elbow grease and it works. Oh and I forgot your program leaks some memory!

This post has been edited by taylorc8: 27 December 2009 - 12:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Random Password Generator

Posted 26 December 2009 - 11:51 PM

Thanks for the quick reply. I am pretty new to programming in visual studio (just finished my first semester in college) and I still really don't know how to use the debug feature. Shame on me, I know :)

That's interesting. The length is passed by reference from main so i'm not quite sure why it is negative. It is passed correctly into that function when i ran the program.

The problem I encountered was that the for loop didn't put in all my random characters and so there is random junk which appears at the end of my password vector when I display it.

This post has been edited by nmeans73: 26 December 2009 - 11:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Random Password Generator

Posted 27 December 2009 - 12:18 AM

Random junk would probably be acessing your array out of bounds. array indices start at 0 thus
int arr[5];
for(int i=0;i<5;i++)
{
  cout << arr[i];
}

will work just fine. <= won't.

 lowerCase = rand() % 25 +97; // should go to 90 for lowercase ??


Here's something I think you'll find useful if you don't already have one.

/* ascii chart */
for(int a=0;a<256;a++)
	{
		std::cout << (char)a << " #" << a << "\t";
	}
	std::cin.get();

I'm currently a student myself.

Oh and, if you didn't change your function any, void lowerAndUpperCase(unsigned int length) is not by reference,void lowerAndUpperCase(unsigned int & length) would work for passing by reference, do you need to modify your length??

This post has been edited by taylorc8: 27 December 2009 - 12:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Random Password Generator

Posted 27 December 2009 - 12:29 AM

Here's something fun for your vector too.
vector<int> v1;
random_shuffle( v1.begin( ), v1.end( ) );


as the name suggests it will shuffle your vector, also strings end with a \0, that may be some type of error you're encountering.

Oh, and your program leaks memory!!!!!!!!!!!
you used "new" without a matching "delete"

This post has been edited by taylorc8: 27 December 2009 - 12:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Random Password Generator

Posted 27 December 2009 - 12:48 AM

*EDIT*
I fixed the memory leaks. It was my first time using pointers and I simply forgot to delete them when I was done.

I don't know why I said that length was passed by reference when it clearly isn't. It must be because it's late.

As for the <= error in my one for loop, thanks for spotting it, it should just be a <. As for the junk in my display, I know why it's there I just don't know how to fix it.

I think the problem lies in my for loop which assembles the password. I obviously don't have all the cases covered because it isn't filling the vector fully. When it goes to display, it is just putting in what is already in the memory location at the end of the vector.

I am curious as to why you use unsigned ints in the for loop as opposed to regular int declarations.

That vector shuffle makes things a lot simpler. I still want to figure out why my for loop isn't processing all the data though.

For good measure, here is the entirety of my code so far (remember, it isn't complete yet :)):

////////////////////////////////////////////////////////////////////
//Purpose: This program generates a pseudo random password of 
//		 selected size and character composition.
////////////////////////////////////////////////////////////////////

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>


using namespace std;

//PROTOTYPES AND CONSTANTS
void getData(int& length, int& composition);
//Gets and validates the user inputted information on the password to be generated
void lowerCase(int length);
//generate a password of lowercase letters and displays it
void lowerAndUpperCase(int length);
//generate a password of lowercase and uppercase letters





int main()
{
	char selection;		  //variable for user input on whether or not to run program again 
	int length, composition; //variables to hold the string length 
							 //and composition of the password

	do
	{//allows users to run the program multiple times
		getData(length, composition);

		if(composition == 1)
			lowerCase(length);

		if(composition == 2)
			lowerAndUpperCase(length);


		cout<<"Would you like to generate another password (y or n)? ";
		cin>>selection;
		cout<<endl<<endl;

		while(selection != 'n' && selection != 'y')
		{
			cout<<"Your choice must either be y or n, try again: ";
			cin>> selection;
			cout<<endl<<endl;
		}

	}
	while(selection == 'y');

	cout<<endl;

	cout<<"All passwords generated are saved in a text file on your desktop \n"
		<<"Thank you and have a nice day. \n\n";


	return 0;
}


//DEFINTIONS SECTION

void getData(int& length, int& composition)
{//function gets and validates the user input

	cout<<"How long would you like your password to be? ";
	cin >>length;
	cout<<endl;

	while(length <= 0)
	{
		cout<<"You entered an unexpected value. \n"
			<<"Please enter the length again: ";
		cin>>length;
		cout<<endl;
	}

	cout<<endl<<endl;

	cout<<"What types of characters would you like your password to be comprised of? \n";
	cout<<endl;
	cout<<"1. Lowercase letters \n"
		<<"2. Lowercase and Uppercase letters \n"
		<<"3. Lowercase letters, Uppercase letters and Numbers \n"
		<<"4. Lowercase letters, Uppercase letters, Numbers and Symbols \n\n";
	cin>>composition;
	cout<<endl<<endl;

	while(composition > 4 || composition < 1)
	{
		cout<<"That is an invalid choice, please select again: \n\n";
		cout<<"1. Lowercase letters \n"
			<<"2. Lowercase and Uppercase letters \n"
			<<"3. Lowercase letters, Uppercase letters and Numbers \n"
			<<"4. Lowercase letters, Uppercase letters, Numbers and Symbols \n\n";
		cin>>composition;
		cout<<endl<<endl;
	}

}

void lowerCase(int length)
{//generates a password of only lowercase characters and displays it

	char letter;			//letter to be placed in the vector
	vector <char> password;

	srand(time(NULL));	  //seeds the generator 

	for(int i = 0; i < length; i++)
	{//builds the password

		letter = rand() % 25 + 97;

		password.push_back(letter);
	}

	cout<<endl;

	cout<<"Your password is: ";

	for(int j=0; j < length; j++)
		cout<<password[j];

	cout<<endl<<endl;

}

void lowerAndUpperCase(int length)
{//generates a password of lower and upper case letters
 //length is user entered in the getData function

	

	char upperCase, lowerCase;		//holds the random character of each case
	int numberCaps, numberLower;	  //the total number of upper/lowercase letters to be generated
	int counterUpper, counterLower;   //counter which will count up to the number of upper/lowercase letters
	int randInt;					  //random number to determine whether an upper or lowercase letter is next

	vector <char> password;		   //holds the resulting password

	counterUpper = 0;				 //used in the for loop where the password is assembled
	counterLower = 0;



	srand(time(NULL));				//seeds the pseudo-random generator

	numberCaps = rand() % length + 1; //initial number of capital letters

	//If the number of capital letters is too small, program will calculate a new number
	while(length > 2 && numberCaps < 2)
		numberCaps = rand() % length + 1;

	while(numberCaps >= ((2*length)/3))
		numberCaps = rand() % length + 1;

	while(numberCaps <= (length/3))
		numberCaps = rand() % length + 1;

	numberLower = length - numberCaps;  //number of lowercase letters is totally dependent on numberCaps

	int upperSize; 
	upperSize = numberCaps;
	int *capsArray = new int [upperSize];
	//array to hold the uppercase letters

	int lowerSize; 
	lowerSize = numberLower;
	int *lowerArray = new int [lowerSize];
	//array to hold the lowercase letters

	for(int i=0; i < numberCaps; i++)
	{//generates uppercase letters
		upperCase = rand() % 25 + 65;

		capsArray[i] = upperCase;
	}

	for(int j=0; j < numberLower; j++)
	{//generates lowercase letters
		lowerCase = rand() % 25 +97;

		lowerArray[j] = lowerCase;
	}

	for(int z=0; z < length; z++)
	{//assembles password vector
	 //ERROR IN HERE SOMEWHERE
	 
		randInt = rand() % 10+1;

		if(randInt <= 5 && counterLower <= numberLower)
			password.push_back (lowerArray[counterLower]);

		else if(randInt >= 6 && counterUpper <= numberCaps )
			password.push_back (capsArray[counterUpper]);

		if(counterUpper > numberCaps)
			password.push_back(lowerArray[counterLower]);

		else if(counterLower > numberLower)
			password.push_back(capsArray[counterUpper]);

		

		counterUpper ++;
		counterLower ++;

	}

	cout<<"Your password is: ";
	
	for(int q=0; q < length; q++)
		cout<<password[q];

	cout<<endl<<endl;

	delete[] capsArray;
	delete[] lowerArray;

}



This post has been edited by nmeans73: 27 December 2009 - 12:52 AM

Was This Post Helpful? 0
  • +
  • -

#8 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Random Password Generator

Posted 27 December 2009 - 01:03 AM

First I fixed the memory leak in yours, then I pressed 4 for all combinations. It didn't work :(
The one I posted above gave me Your password is: nei[M²ôYp
though... Oh and you may want to use a "string" object for getting input from the user. If you type too much stuff in the variable it's another memory error.
string select;
cin >> select;

Would you like to email me, and we could do something with C++ for fun? taylorc8@my.iecc.edu
Was This Post Helpful? 0
  • +
  • -

#9 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Random Password Generator

Posted 27 December 2009 - 01:30 AM

View Posttaylorc8, on 27 Dec, 2009 - 12:03 AM, said:

First I fixed the memory leak in yours, then I pressed 4 for all combinations. It didn't work :(
The one I posted above gave me Your password is: nei[M²ôYp
though... Oh and you may want to use a "string" object for getting input from the user. If you type too much stuff in the variable it's another memory error.


I know that all four don't work because I haven't written all of them yet :D I left the input as a char because I expressly ask for a simple y or n. I'm going to give the shuffle vector thing a try and see if that fixes my problem. If it does, I'll finish the program and try to think of another one to do :D
Was This Post Helpful? 0
  • +
  • -

#10 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Random Password Generator

Posted 27 December 2009 - 01:52 AM

Here's something fun, it may not be the best, but random_shuffle() makes it pretty darn easy.

void uppercase(unsigned int shuffles)
{
	srand((unsigned int)time(0)); // seed for rand based on time
	vector <unsigned int> letters;
	unsigned int iNumShuffle=rand() % shuffles+1;

	/* assign vector A-Z */
	for(unsigned int i=65;i<91;i++)
	{
		letters.push_back((char)i);
	}

	/* shuffle vector */
	for(unsigned int i=0;i<iNumShuffle;i++)
	random_shuffle(letters.begin(),letters.end());

				/* output vector contents */
	for(unsigned int i=0;i<letters.size();i++)
		cout << (char)letters[i];



	return;
}



#include <algorithm>

Well, if HP can do it easier, and put it in a standard library, I'm going to use it!

/*
* This file is derived from software bearing the following
* restrictions:
*
* Copyright © 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Hewlett-Packard Company makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*/

/*
* Copyright © 1992-2007 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V5.03:0009 */

This post has been edited by taylorc8: 27 December 2009 - 01:58 AM

Was This Post Helpful? 1
  • +
  • -

#11 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: Random Password Generator

Posted 27 December 2009 - 03:54 AM

Putting a call to srand inside a function with rand will produce very predictable repeated numbers. srand is designed to be used exactly once during the lifetime of a program; the best place to call it is in main().


Also, strings are usually easier to use as character containers rather than vector<char>
std::string password = "thelazydog";
std::random_shuffle(password.begin(), password.end()); 

This post has been edited by Bench: 27 December 2009 - 03:57 AM

Was This Post Helpful? 1
  • +
  • -

#12 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Random Password Generator

Posted 27 December 2009 - 10:55 AM

View PostBench, on 27 Dec, 2009 - 02:54 AM, said:

Putting a call to srand inside a function with rand will produce very predictable repeated numbers. srand is designed to be used exactly once during the lifetime of a program; the best place to call it is in main().


Also, strings are usually easier to use as character containers rather than vector<char>


Thanks for the advice with srand. I forgot that you were only supposed to seed the timer once. I also totally forgot about using strings. Using strings and random_shuffle really simplified what I have so far and will really help when I design the other password functions.
Was This Post Helpful? 0
  • +
  • -

#13 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Random Password Generator

Posted 29 December 2009 - 12:14 AM

I know that I'm bringing up a dying topic, but I finally finished my program and thought I would post the code here for some final comments/suggestions.

////////////////////////////////////////////////////////////////////
//Purpose: This program generates a pseudo random password of 
//		 selected size and character composition.
////////////////////////////////////////////////////////////////////

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <fstream>


using namespace std;

//PROTOTYPES AND CONSTANTS

void getData(int& length, int& composition);
//Gets and validates the user inputted information on the password to be generated
void lowerCase(int length, ofstream& fout);
//generates a password of lowercase letters and displays it
void lowerAndUpperCase(int length, ofstream& fout);
//generates a password of lowercase and uppercase letters
void lowerUpperAndNum(int length, ofstream& fout);
//generates a password of lower/uppercase letters and numbers
void allOfTheAbove(int length, ofstream& fout);
//generates a password of lower/uppercase letters, numbers and symbols
void lowerAndNum(int length, ofstream& fout);
//generates a password of lowercase letters and numbers





int main()
{
	char selection;		  //variable for user input on whether or not to run program again 
	int length, composition; //variables to hold the string length 
							 //and composition of the password

	string destination;

	ofstream fout;

	cout<<"Enter the path of where you would like the file saved to.\n\n"
		<<"For example: C:/Passwords.txt \n\n";
	getline(cin, destination);

	fout.open(destination.c_str());

	cout<<endl;

	while(!fout)
	{
		cout<<"ERROR!!!"<<endl<<endl;
		cout<<"Please enter a different path: ";
		getline(cin, destination);
		fout.open(destination.c_str());
	}


		
	srand(time(NULL));	   //seeds the pseudo-random generator

	do
	{//allows users to run the program multiple times
		getData(length, composition);

		if(composition == 1)
			lowerCase(length, fout);

		else if(composition == 2)
			lowerAndNum(length, fout);

		else if(composition == 3)
			lowerAndUpperCase(length, fout);

		else if(composition == 4)
			lowerUpperAndNum(length, fout);
	
		else if(composition == 5)
			allOfTheAbove(length, fout);


		cout<<"Would you like to generate another password (y or n)? ";
		cin>>selection;
		cout<<endl<<endl;

		while(selection != 'n' && selection != 'y')
		{
			cout<<"Your choice must either be y or n, try again: ";
			cin>> selection;
			cout<<endl<<endl;
		}

	}
	while(selection == 'y');

	cout<<endl;

	cout<<"Thank you and have a nice day. \n\n";


	return 0;
}


//DEFINTIONS SECTION

void getData(int& length, int& composition)
{//function gets and validates the user input

	cout<<"How long would you like your password to be?\n"
		<<"(must be at least 5 characters long) ";
	cin >>length;
	cout<<endl;

	while(length <= 4)
	{
		cout<<"You entered an unexpected value. \n"
			<<"Please enter the length again: ";
		cin>>length;
		cout<<endl;
	}

	cout<<endl<<endl;

	cout<<"What types of characters would you like your password to be comprised of? \n";
	cout<<endl;
	cout<<"1. Lowercase letters \n"
		<<"2. Lowercase letters and Numbers \n"
		<<"3. Lowercase and Uppercase letters \n"
		<<"4. Lowercase letters, Uppercase letters and Numbers \n"
		<<"5. Lowercase letters, Uppercase letters, Numbers and Symbols \n\n";
	cin>>composition;
	cout<<endl<<endl;

	while(composition > 5 || composition < 1)
	{
		cout<<"That is an invalid choice, please select again: \n\n";
		cout<<"1. Lowercase letters \n"
			<<"2. Lowercase letters and Numbers \n"
			<<"3. Lowercase and Uppercase letters \n"
			<<"4. Lowercase letters, Uppercase letters and Numbers \n"
			<<"5. Lowercase letters, Uppercase letters, Numbers and Symbols \n\n";
		cin>>composition;
		cout<<endl<<endl;
	}

}

void lowerCase(int length, ofstream& fout)
{//generates a password of only lowercase characters and displays it

	char letter;			//letter to be placed in the vector

	string password;		//string to hold the password



	for(int i = 0; i < length; i++)
	{//builds the password

		letter = rand() % 25 + 97;

		password = password + letter;
	}

	for(int a=0; a<1000; a++)
	{//shufles the password string 1000 times

		random_shuffle(password.begin(), password.end());
	}


	cout<<"Your password is: "<<password<<endl<<endl;
	fout<<password<<endl<<endl;
	


}

void lowerAndUpperCase(int length, ofstream& fout)
{//generates a password of lower and upper case letters
 

	
	
	char upperCase, lowerCase;		//holds the random character of each case
	int numberCaps, numberLower;	  //the total number of upper/lowercase letters to be generated

	string upper, lower, password;	//strings to hold the different characters


	numberCaps = rand() % length + 1; //initial number of capital letters

	//If the number of capital letters is too small, program will calculate a new number
	while(length > 2 && numberCaps < 2)
		numberCaps = rand() % length + 1;

	while(numberCaps >= ((2*length)/3))
		numberCaps = rand() % length + 1;

	while(numberCaps <= (length/3))
		numberCaps = rand() % length + 1;


	numberLower = length - numberCaps;  //number of lowercase letters is totally dependent on numberCaps


	for(int i=0; i < numberCaps; i++)
	{//generates uppercase letters
		upperCase = rand() % 25 + 65;

		upper = upper + upperCase;
	}

	for(int j=0; j < numberLower; j++)
	{//generates lowercase letters
		lowerCase = rand() % 25 +97;

		lower = lower + lowerCase;
	}


	password = lower+upper;

	for(int a=0; a<1000; a++)
	{//shuffles the password string 1000 times

		random_shuffle(password.begin(), password.end());	
	}

	cout<<"Your password is: "<<password<<endl<<endl;
	fout<<password<<endl<<endl;


}


void lowerUpperAndNum(int length, ofstream& fout)
{//generates a password of lower/uppercase letters and numbers

	
	char upperCase, lowerCase, randNumber;	   //random upper/lowercase letters/number				 
	int numberUpper, numberLower, numberNumber;  //the total number of each character type
	int whatsLeft;							   //the amount of space left after the number of caps is determined

	string upper, lower, numbers, password;	  //holds all the random numbers/letters and the final password

	
	numberUpper = rand() % length + 1;		   //initial number of capital letters

	//If the number of capital letters is too small, program will calculate a new number
	while(length > 3 && numberUpper < 2)
		numberUpper = rand() % length + 1;

	while(numberUpper >= ((2*length)/3))
		numberUpper = rand() % length + 1;

	while(numberUpper <= (length/3))
		numberUpper = rand() % length + 1;

	whatsLeft = length - numberUpper;			//how many characters are left after the number of caps is known

	numberNumber = rand() % whatsLeft + 1;	   //inital number of numbers in the password

	while(numberNumber >= ((2*whatsLeft)/3))
		numberNumber = rand() % whatsLeft + 1;

	while(numberNumber <= (whatsLeft/3))
		numberNumber = rand() % whatsLeft + 1;

	numberLower = length - (numberUpper+numberNumber);			//number of lowercase letters is totally dependent on numberCaps

	for(int a=0; a<numberUpper; a++)
	{//generates random capital letters

		upperCase = rand() % 25 + 65;

		upper = upper + upperCase;

	}

	for(int b=0; b<numberLower; b++)
	{//generates random lowercase letters

		lowerCase = rand() % 25 + 97;

		lower = lower + lowerCase;

	}

	for(int c=0; c<numberNumber; c++)
	{//generates random numbers

		randNumber = rand() % 9 + 48;
		
		numbers = numbers + randNumber;

	}


	password = numbers + lower + upper;

	for(int d=0; d<1000; d++)
	{//randomizes the combined strings 1000 times

		random_shuffle(password.begin(), password.end());
	}

	cout<<"Your password is: "<<password<<endl<<endl;
	fout<<password<<endl<<endl;
}


void allOfTheAbove(int length, ofstream& fout)
{//generates a random password from all the data types

	
	char upperCase, lowerCase, randNumber, symbol;			 //random upper/lowercase letters/number/symbol				 
	int numberUpper, numberLower, numberNumber, numberSymbol;  //the total number of each character type
	int total;												  //the amount of space taken up by numberUpper-Symbol
	string upper, lower, numbers, symbols, password;		   //holds all the random numbers/letters/symbols 
															   //and the final password

	numberUpper = length/4;
	numberLower = length/4;
	numberNumber= length/4;
	numberSymbol= length/4;

	total = numberUpper + numberLower + numberNumber + numberSymbol; 

	if(total < length)										/*checks to see if the total numbers matches the length
		numberSymbol = (length-total) + numberSymbol;		  if the total is less than the length, the remaining 
															  characters will be more symbols*/



	for(int a=0; a<numberUpper; a++)
	{//generates random capital letters

		upperCase = rand() % 25 + 65;

		upper = upper + upperCase;

	}

	for(int b=0; b<numberLower; b++)
	{//generates random lowercase letters

		lowerCase = rand() % 25 + 97;

		lower = lower + lowerCase;

	}

	for(int c=0; c<numberNumber; c++)
	{//generates random numbers

		randNumber = rand() % 9 + 48;
		
		numbers = numbers + randNumber;

	}

	for(int d=0; d<numberSymbol; d++)
	{//generates random symbols

		symbol = rand() % 92 + 33;

		if(symbol >= 48 && symbol <= 57)
			symbol = rand() % 92 + 33;

		if(symbol >= 65 && symbol <= 90)
			symbol = rand() % 92 + 33;

		if(symbol >= 97 && symbol <= 122)
			symbol = rand() % 92 + 33;

		symbols = symbols + symbol;
	}

	password = numbers + symbols + lower + upper;

	for(int e=0; e<1000; e++)
	{//randomizes the password 1000 times

		random_shuffle(password.begin(), password.end());

	}

	cout<<"Your password is: "<<password<<endl<<endl;
	fout<<password<<endl<<endl;
	
}

void lowerAndNum(int length, ofstream& fout)
{
	char number, lower;			  //number and lowercase letter characters
	int numberLower, numberNumber;	 //holds the total number of each char type

	string lowerCase, numberString, password;

	numberLower = rand() % length + 1;
	numberNumber = length - numberLower;

	for(int i = 0; i < numberLower; i++)
	{//generates random lowercase letters
		lower = rand() % 25 + 97;

		lowerCase = lowerCase + lower;

		
	}

	for(int j=0; j < numberNumber; j++)
	{//generates random numbers
		number = rand() % 9 + 48;

		numberString = numberString + number;

		
	}

	password = lowerCase + numberString;

	for(int a=0; a<1000; a++)
	{//shufles the password string 1000 times

		random_shuffle(password.begin(), password.end());
	}


	cout<<"Your password is: "<<password<<endl<<endl;
	fout<<password<<endl<<endl;

}

This post has been edited by nmeans73: 29 December 2009 - 12:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1