I'm trying to make a password generator using a class just to make things more organized. There was no problem when there was no class. but since I tried to make this class it didn't print out the generated password when it compiled. in my cluelessness I assumed that I had to convert the char in to a string, and I'm not even sure if I'm doing that right please HELP.
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <string.h>
usingnamespace std;
char pwd[] = "abcdefghijklmnopqrstuvwxyz1234567890";
char password[50];
int I, J;
class Generator{
private:
int length;
public:
void setLength(int l){
int length;
length=l;
}
int getTheLength(){
int theLength;
theLength=length;
return theLength;
}
int genNumber(){
string thePass;
srand(time(NULL));
for(int I=0; I<getTheLength(); I++) {
password[I] = pwd[rand()%37];
}
for(int J=0; J<getTheLength(); J++) {
//cout<<password[J];
}
return password.c_str();
//strcpy(password, thePass.c_str());
//return thePass;
}
};
and here is the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "C:\Users\Isaac Dixon\Documents\School\Oak Wood University\c++ data structchers\generator.h"
int main() {
Generator obj1;
int a, thePass;
cout << " how long is the password? :";
cin >> a;
obj1.setLength(a);
cout << " your password:";
obj1.genNumber();
thePass=obj1.genNumber();
return 0;
}
#include <iostream>
#include <string> // new header
#include <ctime> // was time.h
#include <cstdlib>
#include <cstring> // was string.h
usingnamespace std; // bad idea to use in header
//char pwd[] = "abcdefghijklmnopqrstuvwxyz1234567890"; doesn't need to be global
//char password[50]; // not needed
//int I, J; // not needed
class PasswordGenerator{ // was just Generator
private:
int length;
public:
void setLength(int l){
//int length; hides member varaible
length=l;
}
int getTheLength(){
//int theLength; waste of space!
//theLength=length;
//return theLength;
return length;
}
// was getNumber
// was returning int
string genPassword(){ // why genNumber when it's generating a password?
staticconstchar pwd[] = "abcdefghijklmnopqrstuvwxyz1234567890";
string thePass;
//srand(time(NULL)); call at start of program
for(int I=0; I<getTheLength(); I++) {
//password[I] = pwd[rand()%36]; // was 37 (26 letter + 10 numbers = 36)
thePass += pwd[rand()%36]; // for array of length 36 safe indices are 0-35
}
//for(int J=0; J<getTheLength(); J++) {
// cout<<thePass[J];
//}
//return password.c_str();
//strcpy(password, thePass.c_str());
return thePass; // restore this
}
};
//#include "C:\Users\Isaac Dixon\Documents\School\Oak Wood University\c++ data structchers\generator.h"
#include "generator.h"
int main() {
srand(time(NULL)); // moved here!
PasswordGenerator obj1;
//int a, thePass;
int len = 0; // better name/init for safety
string thePass;
cout << " how long is the password? :";
cin >> len;
obj1.setLength(len); // not sure why a property? could pass as parameter to genPassword
//cout << " your password:";
//obj1.genNumber(); pointless call (return not used)
thePass = obj1.genPassword(); // was genNumber
cout << "your password:" << thePass << "\n";
return 0;
}
<ctime> and <cstring> are the C++ versions of the C Standard Library headers <time.h> and <string.h>. You were already using <cstdlib> which is C++ version of <stdlib.h>
The C++ files wrap the C functions in the standard namespace, which can help resolve ambiguities, and in some cases also provide additional functionality (inc. overloads of some functions, particularly maths functions in <cmath>.)