PasswordFile Program

I am getting this undefined reference to `PasswordFile::addpw(UserPw)'
I am just getting back into c++ and I know it has something to do with the UserPW I am calling I believe? Any hints much appreciated. I am basically using a text file to store user name and password along with inserting new ones into the 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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class UserPw
{
 public:
 UserPw(string user, string password) //Constructor
  {
	this->user = user;
	this->password = password;
  }//Constructor
 string GetUser(); // returns the user
 bool Checkpw(string user, string passwd); // returns true if user and password both match

 private :
 string user, password;

};

class PasswordFile
{
 public:
 PasswordFile(string filename);// opens the file and reads the names/passwords in the vector entry
 vector<UserPw> getFile(); // returns the vector entry 
 void addpw(UserPw newentry); //this adds a new user/password to entry and writes entry to the file filename
 bool checkpw(string user, string passwd); // returns true is user exists and password matches

 private:
 string filename; // the file that contains password information
 vector<UserPw> entry; // the list of usernames/passwords
};

PasswordFile::PasswordFile(string filename)
{
  this->filename = filename;  //points to this filename
  string user, password;
  ifstream pfile;
  pfile.open(this->filename.c_str());
  pfile >> user >> password;

  while (pfile.good())
  {
    entry.push_back(UserPw(user, password));
    pfile >> user >> password;
  }
} // end of PasswordFile:PasswordFile(string filename)

int main()
{
 PasswordFile passfile("password.txt");
 passfile.addpw(UserPw("dbotting","123qwe"));
 passfile.addpw(UserPw("egomez","qwerty"));
 passfile.addpw(UserPw("tongyu","liberty"));
 // write some lines to see if passwords match users
}
closed account (48T7M4Gy)
You have the prototype for addPW at line 29 but nothing else. addPW doesn't know what it is supposed to do.
Forgive me, I believe I get what you are saying. It needs to have a member function correct?

1
2
3
4
PasswordFile:: void addpw(UserPw newentry)
{
  //some code to add user and password to file  which I thought line 45 to 49 did?
}
closed account (48T7M4Gy)
yes
So then I just need to move the code from lines 45 - 49 in the above code to this member function?
closed account (48T7M4Gy)
try it :-)
So I got the file to accept the values for the username and password. So I am not sure how I would go about checking that the password matches the username.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class UserPw
{
 public:
 UserPw(string user, string password); //Constructor
 string GetUser(); // returns the user
 string GetPassword(); // returns the password
 bool Checkpw(string user, string passwd); // returns true if user and password both match

 private :
 string user, password;

};

UserPw::UserPw(string user, string password)
{
  this-> user = user;
  this-> password = password;
}

string UserPw::GetUser()
{
  return user;
}

string UserPw::GetPassword()
{
  return password;
}

bool UserPw::Checkpw(string user, string passwd) // add this part in need to go through it more
{
  this -> user = user;
  this -> passwd = passwd;

  
}

class PasswordFile
{
 public:
 PasswordFile(string filename);// opens the file and reads the names/passwords in the vector entry
 vector<UserPw> getFile(); // returns the vector entry 
 void addpw(UserPw newentry); //this adds a new user/password to entry and writes entry to the file filename
 bool checkpw(string user, string passwd); // returns true is user exists and password matches

 private:
 string filename; // the file that contains password information
 vector<UserPw> entry; // the list of usernames/passwords
 void write();
};

PasswordFile::PasswordFile(string filename)
{
  this->filename = filename;  //points to this filename
  string user, password;
  ifstream pfile;
  pfile.open(this->filename.c_str());
  pfile >> user >> password;

  while (pfile.good())
  {
    entry.push_back(UserPw(user, password));
    pfile >> user >> password;
  }
} // end of PasswordFile:PasswordFile(string filename)

vector <UserPw> PasswordFile::getFile()
{
  return getFile();
}

void PasswordFile:: addpw(UserPw newentry)
{
  entry.push_back(newentry);
  write();

}

void PasswordFile::write()
{
  ofstream outfile;
  outfile.open(filename.c_str());

  for (int i = 0; i < entry.size(); i++)
  {
    outfile << entry[i].GetUser() << " " << entry[i].GetPassword() << endl;
  }  
  outfile.close();
}

/*bool PasswordFile::checkpw(string user, string password)
{
  
}*/

int main()
{
 PasswordFile passfile("password.txt");
 passfile.addpw(UserPw("dbotting","123qwe"));
 passfile.addpw(UserPw("egomez","qwerty"));
 passfile.addpw(UserPw("tongyu","liberty"));
 // write some lines to see if passwords match users

//bool checkpw(string user, string passwd);

}
closed account (48T7M4Gy)
You'll need to have the first go here.

To help you though the answer to a large extent is written in the function template presumable given to you as part of the assignment. Feed the strings to the function and it comes back with a bool true or false. All you have to do is check whether the two strings are equal. If they are then the answer is true otherwise the answer returned is false.
Last edited on
Awesome thanks,

So now I get this error in this line of code says "expected primary-expression before ']' token" ??? hints?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 bool PasswordFile::checkpw(string user, string password)
  {
   for(int i = 0; i < entry.size(); i++)
   {
    if(entry[UserPw].getuser() == user && entry[UserPw].getpassword == password)
     {
       return true;
     }
    else
     {
       return false;
     }
   }
  }
closed account (48T7M4Gy)
Yeah, two points:

- when you get an error the error message will probably include the line number. It would certainly help. ;)

- However despite this, I'm reasonably sure it's because you haven't defined what UserPW is. I guess you mean to say something like, string UserPW = user; then int userNo = user;

With that info the function can then proceed with checking each entry in 'passfile' (hint) and returning the appropriate bool value.

However, (strong hint) you must also note that you cant refer to entry [UserPW] as it stands in your code because the variable in brackets can't be a string!
It might be better to post all I have now, instead of a section to better understand. The error I get is at line 106 if you didn't already know. What you were talking about isn't that the string UserPw:GetUser() aka line 27? Thanks again for the help so far.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class UserPw
{
 public:
 UserPw(string user, string password); //Constructor
 string GetUser(); // returns the user
 string GetPasswd(); // returns the password
 bool Checkpw(string user, string passwd); // returns true if user and password both match

 private :
 string user, password;

};

UserPw::UserPw(string user, string password)
{
  this-> user = user;
  this-> password = password;
}

string UserPw::GetUser()
{
  return user;
}

string UserPw::GetPasswd()
{
  return password;
}

bool UserPw::Checkpw(string user, string passwd) // add this part in need to go through it more
{
  if(user == user && password == passwd)
  {
    return true;
  }
  else
  {
    return false;
  }
}

class PasswordFile
{
 public:
 PasswordFile(string filename);// opens the file and reads the names/passwords in the vector entry
 vector<UserPw> getFile(); // returns the vector entry
 void addpw(UserPw newentry); //this adds a new user/password to entry and writes entry to the file filename
 bool checkpw(string user, string passwd); // returns true is user exists and password matches

 private:
 string filename; // the file that contains password information
 vector<UserPw> entry; // the list of usernames/passwords
 void write();
};

PasswordFile::PasswordFile(string filename)
{
  this->filename = filename;  //points to this filename
  string user, password;
  ifstream pfile;
  pfile.open(this->filename.c_str());
  pfile >> user >> password;

  while (pfile.good())
  {
    entry.push_back(UserPw(user, password));
    pfile >> user >> password;
  }
} // end of PasswordFile:PasswordFile(string filename)

vector <UserPw> PasswordFile::getFile()
{
  return getFile();
}

void PasswordFile:: addpw(UserPw newentry)
{
  entry.push_back(newentry);
  write();

}

void PasswordFile::write()
{
  ofstream outfile;
  outfile.open(filename.c_str());

  for (int i = 0; i < entry.size(); i++)
  {
    outfile << entry[i].GetUser() << " " << entry[i].GetPasswd() << endl;
  }
  outfile.close();
}

bool PasswordFile::checkpw(string user, string passwd)
{
  for(int i = 0; i < entry.size(); i++)
  {
    if(entry[UserPw].getuser() == user && entry[UserPw].getpasswd() == passwd)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}

int main()
{
 PasswordFile passfile("password.txt");
 passfile.addpw(UserPw("dbotting","123qwe"));
 passfile.addpw(UserPw("egomez","qwerty"));
 passfile.addpw(UserPw("tongyu","liberty"));
 // write some lines to see if passwords match users

//bool checkpw(string user, string passwd);

}
closed account (48T7M4Gy)
I'll give it a whirl. Wait a few minutes.

Well, you haven't defined what UserPW is in the function. I think you mean it to be i.

And, for the next errors my hint is uppercase/lowercase.

if(entry[i].GetUser() == user && entry[i].GetPasswd() == passwd)

And in main() a simple test is:
std::cout << passfile.checkpw("dbotting", "123qwe") << endl;
Last edited on
For some reason it is not actually checking the passwords. I moved the function up because I thought it was closing the file before it actually checked but that was not it.. ?
closed account (48T7M4Gy)
if(entry[i].GetUser() == user && entry[i].GetPasswd() == passwd)
Yeah I have that looks like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool PasswordFile::checkpw(string user, string passwd)
{
  for(int i = 0; i < entry.size(); i++)
  {
    if(entry[i].GetUser() == user && entry[i].GetPasswd() == passwd)
    {
      return true;
      cout << "User/password match" << endl;
    }
    else
    {
      return false;
      cout << "User/password does not match" << endl;
    }
  }
}


It almost like it is never hitting this function?
Last edited on
closed account (48T7M4Gy)
You have to make the changes I suggested then add a small amout of code in main() to test it eg:

1
2
3
4
5
6
7
if (passfile.checkpw("dbotting", "123qwe") == true)
		cout << "Bingo\n";

	if (passfile.checkpw("tongu", "123qwe") == true)
		cout << "Bingo\n";
	else
		cout << "Bad Luck.\n";


Last edited on
Okay so I did as mentioned adding that into main but for some reason it will not throw that cout<< I put in that function?
Last edited on
Nvm I fixed..... *face palm*

Thank you for the help.
Last edited on
closed account (48T7M4Gy)
I also noticed you have two checking functions, checkpw and Checkpw. Scrap the latter!
Topic archived. No new replies allowed.