i can't return the value!!!?

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.

here is the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
#include <time.h>
#include <cstdlib>
#include <string.h>
using namespace 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;
}
Last edited on
More than one way to convert int to string

if your compiler supports it.
std::string s = std::to_string(42);

if your compiler like mine dose not see to_string() then you have other options.
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c

The method I choose to use looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <stdlib.h>
#include <sstream>


using namespace std;

string IntToString (int a)
{
    ostringstream temp;
    temp<<a;
    return temp.str();
}


int main() {

    int len = 15;
    string randomNumString;

    srand(time(NULL));
    for (int i = 0; i < len; ++i) {

        randomNumString += IntToString ((rand()%10));
    }
    cout << randomNumString << endl;

    return 0;
}

Last edited on
I don't get why you are trying to return an int from a function called getNumber when your code is creating a random string.

I think you need to study the tutorials (here and elsewhere) and then have another go!

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string> // new header
#include <ctime> // was time.h
#include <cstdlib>
#include <cstring> // was string.h
using namespace 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?
        static const char 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
    }
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//#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;
}
Last edited on
And as single file for C++ Shell...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;

class PasswordGenerator {
private: 
    int length;

public:
    void setLength(int l) {
        length=l;
    }

    int getTheLength() const {
        return length;
    }

    string genPassword() const {
        static const char pwd[] = "abcdefghijklmnopqrstuvwxyz1234567890";
        static const int  cnt   = sizeof(pwd)/sizeof(pwd[0]);
        string thePass;
        for(int I=0; I<getTheLength(); ++I)
            thePass += pwd[rand()%cnt];
        return thePass;
    }
};

int main() {
    srand(time(NULL));

    int len = 0;
    cout << " how long is the password? :";
    cin >> len;

    PasswordGenerator pwgen;
    pwgen.setLength(len);
    string thePass = pwgen.genPassword();
    cout << "your password:" << thePass << "\n";

    return 0;
}
Thank you so much Andy, ill defiantly study those things vary hard

and when I was tiring return an int from a function that was suppose to create a random string that was a dumb mistake I made sorry about that.


on the other had can you please explain why you switched #include <ctime> with #include <time.h> and #include <string.h> whith #include <cstring> ?
Last edited on
<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>.)

Andy
I got it thank you
Topic archived. No new replies allowed.