![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
looks good
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|