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