If statement function (inside a class) with private strings

My issue is stated down in the comments, right at the beggining of the code:

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
//I am trying to create a program that tells the user to enter a password, stores it in a private string and then gets the user to re-enter the password, and if the re-written password, rePass (that is equal to testPass) is equal to password, print out "You are in." on the screen.
//Any advice? The bug is at the if statement. I kinda want to solve the bug myself, so i don't want the solution, just a hint.
//I have removed the return testPass; that was in the getPassword function, as you can only return once; But I belive, pardon me if I'm wrong, I need to return both password and testPass to be able to use them in an if statement.
 
 
#include <iostream>
#include <string>
using namespace std;
 
class passwordAssignment{
    public:
        void printMsg(){
            cout << "Enter the password. \n\n";
        }
        void setPassword(){
                string pass;
                cout << "Password: ";
                cin >> pass;
                cout << "\n";
                password = pass;
        }
        string getPassword(){
                string rePass;
                cout << "Enter the password you just typed. \n\n";
                cout << "Password: ";
                cin >> rePass;
                testPass = rePass;
                return password;
        }
        void testPassword(){
                if (testPass = password){
                        cout << "You are in.";
                }else{
                            cout << "That was not the password you entered at first.";
            }
        }
    private:
        string testPass;
        string password;
};
 
 
int main()
{
    passwordAssignment testObject;
   
    testObject.printMsg();
    testObject.setPassword();
    testObject.getPassword();
    testObject.testPassword();
   
    return 0;
}
closed account (48T7M4Gy)
Hint:

You are mixing input and output (cin and cout) in your class rather than in main() the driver. This is not good programming practice and makes debugging very difficult. You need 'gets and sets' methods in the class free of cin's and cout's and a bool return value in another class method which verifies the password.

Why return password to main but never use it - line 49?

- as an aside, 'passwordAssignment' is not a very descriptive class name

I need to return both password and testPass

So have two separate methods.

:)
Last edited on
Topic archived. No new replies allowed.