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;
}