Header Files and Private Variable Usage

Hello! I'm trying to create a header file for my upcoming project. This is my first time actually using classes so I'm not quite clear on all the do's and don't's of header files and classes. My teacher said it was alright to define the private functions, setters, and getters in the header file, but when I went to do that, I had a recurring error in my getter functions. They're supposed to return the private variables and that's it, but having the function do just that returns errors. In line 14 I tried a solution that removed the errors based off a stack overflow page where they added an underscore before the variable name. I'm worried, however, that if I do this, then when I use the function in my .cpp, it will expect me to provide that variable, when it already exists in the class and all I'm doing is changing it with the setter and retrieving its data with the setter. Any clarification on this matter would be much appreciated!

Here's the header file
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef PASSWORDMANAGER_H
#define PASSWORDMANAGER_H
#include <string>
#include <ctype.h>
class PasswordManager
{
        public:
        PasswordManager();
        //Sets username to input
        void setUsername(std::string input){
            username = input;
        };
        //Returns the stored username
        std::string getUsername(std::string _username){
            return username;
        };
        //Sets encryptedPass to the encrypted input
        void setEncryptedPassword(std::string input){
            encryptedPass = input;
        };
        //Returns the stored EncryptedPass
        std::string getEncryptedPass{
            return encryptedPass;
        };
        //Takes a new proposed password, checks if it meets criteria, and then encrypts the password and stores it in the member variable
        bool setNewPassword(std::string input){
            if(meetsCriteria(input) == true){
                encryptedPass = encrypt(input);
                return true;
            }
            else return false;
        };
        bool authenticate(std::string input){
            std::string temp = encrypt(input);
            if(encryptedPass == temp)
                return true;
            else return false;
        }
};
        virtual ~PasswordManager();

    private:
        std::string username,
               encryptedPass;
        //Encrypts the password, returns encrypted password
        std::string encrypt(std::string password){
            std::string output = password;
            for(int i = 0; i < password.size(); i++)
                output[i] = ((password[i]-33)+45)%94+33;
        return output;
        }
        //If the password doesn't meet criteria, returns false
        bool meetsCriteria(std::string password){
            bool criteriaMet = true,
                 check = false;
            if(password.length() < 9)
                criteriaMet = false;
            for(int i=0;i<password.size();i++){
                if(islower(password[i]) != 0)
                    check = true;
            }
            if(check = false)
                criteriaMet = false;
            if(password[0] = '_')
                criteriaMet = false;
    return criteriaMet;
    }


#endif // PASSWORDMANAGER_H 

Edit: Finished the header so once the problems are resolved I can move to the driver
Last edited on
You are confusing = and ==.
= is assignment
== checks for equality

1
2
3
4
        //Returns the stored username
        std::string getUsername(std::string _username){
            return username;
        };
It's not an error but you aren't using your _username variable.

1
2
3
4
        //Sets encryptedPass to the encrypted input
        void setEncryptedPassword(std::string input){
            encryptedPass = input;
        };
I don't see the point of this function. Why let the user directly set the value of encryptedPass? The value of encryptedPass is set inside setNewPassword already.

Why have a virtual destructor if you aren't using any sort of polymorphism, by the looks of it?

Line 64: You are potentially going out of bounds if password is an empty string.
Last edited on
Most places I'm using == I'm using it in conjunction with the bool output of meetsCriteria to determine something. The virtual destructor was put there by the IDE when I created the header file so I will staunchly not touch it for fear something breaks. My question is what I can do to transform my getter functions so that they a) don't have compile errors and b) return their respective private variable, either username or encryptedPass.
Hello malibuwiley,

A little different format than you have, but read the comments, those that begin with (<---).
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef PASSWORDMANAGER_H
#define PASSWORDMANAGER_H
#include <string>
#include <ctype.h>  // <--- Use <cctype>.

class PasswordManager
{
    public:
        PasswordManager();
        //Sets username to input
        void setUsername(std::string input)
        {
            username = input;
        };  // <--- What is this for? You do not need it. I do not think it makes any difference, but it is not necessary.

        //Returns the stored username
        std::string getUsername(std::string _username)  // <--- Get functions do not normally use any parameters.
        {
            return username;
        };  // <--- What is this for? You do not need it.

        //Sets encryptedPass to the encrypted input
        void setEncryptedPassword(std::string input)
        {
            encryptedPass = input;
        };  // <--- What is this for? You do not need it.

        //Returns the stored EncryptedPass
        std::string getEncryptedPass  // <--- Be consistent with your {}s.
        {
            return encryptedPass;
        };  // <--- What is this for? You do not need it.

        //Takes a new proposed password, checks if it meets criteria, and then encrypts the password and stores it in the member variable
        bool setNewPassword(std::string input)
        {
            if (meetsCriteria(input) == true)
            {
                encryptedPass = encrypt(input);
                return true;
            }
            else return false;
        };  // <--- What is this for? You do not need it.

        bool authenticate(std::string input)
        {
            std::string temp = encrypt(input);
            if (encryptedPass == temp)
                return true;
            else return false;
        }
};  // <--- This closes the class. What follows is not part of the class.

virtual ~PasswordManager();

        private:
            std::string username, encryptedPass;

            //Encrypts the password, returns encrypted password
            std::string encrypt(std::string password)
            {
                std::string output = password;
                for (int i = 0; i < password.size(); i++)
                    output[i] = ((password[i] - 33) + 45) % 94 + 33;
                return output;
            }

            //If the password doesn't meet criteria, returns false
            bool meetsCriteria(std::string password)
            {
                bool criteriaMet = true, check = false;

                if (password.length() < 9)
                    criteriaMet = false;

                for (int i = 0; i < password.size(); i++)
                {
                    if (islower(password[i]) != 0)
                        check = true;
                }

                if (check = false)  // <--- This is a set not a compare.
                    criteriaMet = false;

                if (password[0] = '_')  // <--- This sets password[0] to '_'. is that what you want?
                    criteriaMet = false;

                return criteriaMet;
            }

#endif // PASSWORDMANAGER_H  


In the "getUsername" function you are sending the function a variable, but never use it and the variable that you do use should produce a compiler error that the variable is undefined. See line 52.

Andy
Hey Andy. I cleaned up everything you mentioned. The getUsername function was my attempt at solving the error codes, which I forgot to add. Here they are now:
Lines 16,24 error: expected ';' at end of member declaration
Lines 15,23 error: expected '}' before return
Lines 15,23 error: could not convert '{<expression error>}' from '<brace-enclosed initializer list>' to 'std::string {aka std::basic_string<char>}'|
Lines 15,23 error: expected ';' before return
Lines 15,23 error: expected primary-expression before 'return'
It really doesn't like that I'm just returning an input and doing nothing else but it's my only outlet to bring those two variables to use where I need them. I can change the variables rn but if the compiler throws a fit then I have no way to output them. The project I have is very clear that the only thing these functions are supposed to do is return the private variables.
Hello malibuwiley,

Since I have no way of checking this code right now all I can tell you is to try this ans let me know what happens.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef PASSWORDMANAGER_H
#define PASSWORDMANAGER_H
#include <string>
#include <ctype.h>  // <--- Use <cctype>.

class PasswordManager
{
    public:
        PasswordManager();
        //Sets username to input
        void setUsername(std::string input)
        {
            username = input;
        }

        //Returns the stored username
        std::string getUsername()
        {
            return username;
        }

        //Sets encryptedPass to the encrypted input
        void setEncryptedPassword(std::string input)
        {
            encryptedPass = input;
        }

        //Returns the stored EncryptedPass
        std::string getEncryptedPass()  // <--- Added () at the end.
        {
            return encryptedPass;
        }

        //Takes a new proposed password, checks if it meets criteria, and then encrypts the password and stores it in the member variable
        bool setNewPassword(std::string input)
        {
            if (meetsCriteria(input))  // <--- This function call already returns a bool. No need to check it against true or false.
            {
                encryptedPass = encrypt(input);
                return true;
            }
            else return false;
        }

        bool authenticate(std::string input)
        {
            std::string temp = encrypt(input);

            if (encryptedPass == temp)
                return true;

            else return false;
        }

    virtual ~PasswordManager();

    private:
        std::string username, encryptedPass;

        //Encrypts the password, returns encrypted password
        std::string encrypt(std::string password)
        {
            std::string output = password;

            for (int i = 0; i < password.size(); i++)
                output[i] = ((password[i] - 33) + 45) % 94 + 33;

            return output;
        }

        //If the password doesn't meet criteria, returns false
        bool meetsCriteria(std::string password)
        {
            bool criteriaMet = true,
                check = false;

            if (password.length() < 9)
                criteriaMet = false;

            for (int i = 0; i < password.size(); i++)
            {
                if (islower(password[i]) != 0)
                    check = true;
            }

            if (check = false)
                criteriaMet = false;

            if (password[0] = '_')
                criteriaMet = false;

            return criteriaMet;
        }
};  // <--- This closes the class.

#endif // PASSWORDMANAGER_H  


Andy
Yeah, that's exactly what worked. I guess I should be more careful about including parentheses. Thought I didn't need them if I didn't actually have a variable being input but I guess that's not the case. Thank you very much!
Hello malibuwiley,

Just because a function does not have any parameter passed to it does not mean that you can omit the ().

The () are what makes it a function.

You have 2 for loops that go something like this: for (int i = 0; i < password.size(); i++). The ".size()" function returns a "size_t" variable type, so the loop iterator should match in type. for (size_t i = 0; i < password.size(); i++). Using an "int" will produce a compiler warning, but will not stop the program from compiling or running. It is best when the types match in a case like this.

Andy
R L86 L89 above, == is equality test, = is assignment. Also pass string by ref, not by value unless a copy is required.

Consider:

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
48
49
50
51
#include <string>
#include <cctype>

class PasswordManager
{
public:
	PasswordManager() {}
	void setUsername(const std::string& input) { username = input; }
	std::string getUsername() const { return username; }

	//void setEncryptedPassword(const std::string& input) { encryptedPass = input; }	// NO Use setNewPassword
	std::string getEncryptedPass() const { return encryptedPass; }

	bool setNewPassword(const std::string& input)
	{
		return meetsCriteria(input) ? !(encryptedPass = encrypt(input)).empty() : false;
	}

	bool authenticate(const std::string& input)
	{
		return encryptedPass == encrypt(input);
	}

	//virtual ~PasswordManager();	NOT NEEDED

private:
	std::string username, encryptedPass;

	std::string encrypt(std::string password)	// BY VALUE FOR THE COPY
	{
		for (auto& p : password)
			p = ((p - 33) + 45) % 94 + 33;

		return password;
	}

	bool meetsCriteria(const std::string& password)
	{
		static const size_t MINPASSLEN {9};

		bool criteriaMet {password.length() >= MINPASSLEN};
		bool check {};

		for (size_t i = 0; criteriaMet && !check && i < password.size(); ++i)
			check = islower(static_cast<unsigned int>(password[i])) != 0;

		criteriaMet &= check & (password[0] == '_');

		return criteriaMet;
	}
};

Last edited on
Registered users can post here. Sign in or register to post.