Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 15th, 2005, 5:34 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
Just a small password generator

Not exactly a project, but it was a nice beginner's exercise for me.

I am posting it so that it may be of use to people also learning C++

Basically, you enter a number, and it generates a password with lower and uppercase letters with that many letters.

I am trying to figure out how to include numbers too, but it is a challenge because one can only fit one type in an array.

Also keep in mind that it is so long because I added in dynamic memory allocation ( so I needed a destructor ), a copy constructor, and overloaded assignment operator, and a few other things. I will probably also be adding a function that returns the password's length ( which should be quite easy considering that I already use a length variable in the class. )

//generate.cpp -- generate a password.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>

using namespace std;

class password
{
	private:
		char *pass; //pointer to password
		int length; //length of password
		static char letters[52]; //array of alphabet to gen. password
		static int count;
	public:
		password(int); //constructor
		password(const password &); //copy constructor
		password &operator=(const password &); //assignment operator
		~password();//deconstructor
		void generate(); //generate the password
		friend void set(); //initialize the static array to correct value
		friend ostream &operator<<(ostream &, password &); //overload the "<<" operator
		friend void countPasses();
		
};

char password::letters[52];

int password::count = 0;

void set()
{
	char alpha = 'a';

	for(int x=0;x<26;x++,alpha++)
		password::letters[x] = alpha ;

	alpha = 'A';
	for(int x=26;x<52;x++,alpha++)
		password::letters[x] = alpha;
}//assign a, b, c, d, e, etc
 
password::password(int x) //x = user set password length
{
	pass = new char[x]; // set pointer to newly set array 
	length = x; //set password::length to x;
	++count;
}

password::password(const password &passwd)
{
	++count; //increase count by 1
	
	pass = new char[passwd.length]; //allot new space, and set pointer
	strcpy(pass, passwd.pass); //copy strings
	length = passwd.length; //copy lengths
}

void password::generate()
{
	int val1, val2, point, temp1;
	char temp2;

	for(int i=0;i<length;i++) //repeat for the length of the password
	{
		
		val1 = rand() % 26; //generate two random values
		val2 = rand() % 26; //from 1 - 26
		point = val1 + val2; //add them to get final random value

		*(pass + i) = password::letters[point]; //assign random letter to appropriate password
	}

	*(pass + length) = '\0';
}

ostream &operator<<(ostream &os, password &passwd)
{
	os << passwd.pass; //overload cout 

	return os;
}

password::~password()
{
	delete [] pass; //delete value pointed to by pointer
	--count;
}

void countPasses()
{
	cout << password::count << endl;
}

password &password::operator=(const password &pass2)
{
	if(this == &pass2) //if assigned to eachother
		return *this;

	delete [] pass; //free old password
	length = pass2.length;
	pass = new char[length];
	strcpy(pass, pass2.pass);

	return *this;
}

//short main() to demonstrate class
int main()
{
	srand(time(0)); //seed randomizer
        set(); //set static letters
	int numberof;
	cout << "Enter the number of letters in new password: ";
	cin  >> numberof;
	password pass1(numberof);
	pass1.generate();

	cout << "\n\nPassword: " << pass1 << endl;

	return 0;
}
Jessehk is offline   Reply With Quote
Old Sep 16th, 2005, 1:02 AM   #2
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Looking good, nice and object oriented.

Keep it up.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Sep 16th, 2005, 8:34 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
looks good
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 16th, 2005, 8:41 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
For numbers, put in the alphabetic representation of a digit. '1' is like 'A', not like 1.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC