I wrote a simplified version of the code I'm working on to illustrate a problem I'm having. I think something is wrong in main() that causes the showUsers() method to output the same Login/Password combo for every element (always the last one added).
Here is my code:
#include <iostream>
using namespace std;
//=============================================================================
class AccountInfo {
private:
char* _username;
char* _password;
public:
AccountInfo();
AccountInfo(char* username, char* password);
~AccountInfo();
void setUsername(char* username);
void setPassword(char* password);
char* getUsername();
char* getPassword();
friend ostream& operator<<(ostream& out, AccountInfo& x) {
out << "Login: " << x.getUsername() << endl
<< "Password: " << x.getPassword() << endl;
return out;
}
};
AccountInfo::AccountInfo() {
_username = "";
_password = "";
}
AccountInfo::AccountInfo(char* username, char* password) {
_username = username;
_password = password;
}
void AccountInfo::setUsername(char* username) {
_username = username;
}
void AccountInfo::setPassword(char* password) {
_password = password;
}
char* AccountInfo::getUsername() {
return _username;
}
char* AccountInfo::getPassword() {
return _password;
}
//=============================================================================
class UsersDB {
private:
int _size;
AccountInfo* _accounts[200];
public:
UsersDB();
~UsersDB();
int getSize();
void addUser(AccountInfo* newUser);
void showUsers();
};
UsersDB::UsersDB() {
_size = 0;
}
UsersDB::~UsersDB() {
delete[] _accounts;
}
int UsersDB::getSize() {
return _size;
}
void UsersDB::addUser(AccountInfo* newUser) {
_accounts[_size] = newUser;
_size++;
}
void UsersDB::showUsers() {
for (int i = 0; i < _size; i++) {
cout << *_accounts[i] << endl;
}
}
//---------------------------------------------------------emptyString function
void emptyString(char* token, int size) {
for (int i=0; i < size; i++) token[i] = '\0';
}
//----------------------------------------------------------copyString function
void copyString (char* from, char* to, int size) {
to = new char[size+1];
for (int i=0; i < size; i++) to[i] = from[i];
to[size] = '\0';
}
//--------------------------------------------------------getNextToken function
int getNextToken(char* buffer, char* token, int startPos,
int bufSize, int tokenSize, char delimeter) {
int i, j;
emptyString (token, tokenSize);
i = startPos;
j = 0;
while ((buffer[i] == ' ') && (i < bufSize)) i++; //skipblanks
if (i < 256) {
while ((buffer[i] != delimeter) && (i < 256) && (j < tokenSize))
token[j++] = buffer[i++];
}
return i;
}
//=============================================================================
int main() {
char buffer[256];
char userLoginName[9];
char password[17];
int i, j, k;
char flag[3];;
char command[11];
char blank = ' ';
UsersDB* users = new UsersDB();
AccountInfo* tempAccount;
while (!cin.eof()) { //while end of line is not reached
cin.getline(buffer, 256);
k = getNextToken(buffer, command, 0, 256, 10, blank);
if (command[0] == 'a') {
tempAccount = new AccountInfo();
k = getNextToken(buffer, userLoginName, k, 256, 8, blank);
(*tempAccount).setUsername(userLoginName);
k = getNextToken(buffer, flag, k, 256, 2, blank);
if (flag[1] == 'p') {
k = getNextToken(buffer, password, k, 256, 16, blank);
(*tempAccount).setPassword(password);
}
cout << *tempAccount << endl;
(*users).addUser(tempAccount);
}
else if (command[0] == 's') {
(*users).showUsers();
}
else cout << "Command not found." << endl;
}
return 0;
}
The output looks like this:
=============================================================================== >adduser bob -p password1 Login: bob Password: password1 >adduser jack -p mypassword Login: jack Password: mypassword >adduser jill -p pass1234 Login: jill Password: pass1234 >showusers Login: jill Password: pass1234 Login: jill Password: pass1234 Login: jill Password: pass1234 ===============================================================================
The output SHOULD look like this:
=============================================================================== >adduser bob -p password1 Login: bob Password: password1 >adduser jack -p mypassword Login: jack Password: mypassword >adduser jill -p pass1234 Login: jill Password: pass1234 >showusers Login: bob Password: password1 Login: jack Password: mypassword Login: jill Password: pass1234 ===============================================================================
Note: When I alter main() (by passing the info directly instead of getting it from the console using cin) to look like this:
//=============================================================================
int main() {
UsersDB* users = new UsersDB();
AccountInfo* tempAccount;
tempAccount = new AccountInfo("jack", "mypassword");
(*users).addUser(tempAccount);
tempAccount = new AccountInfo("jill", "pass1234");
(*users).addUser(tempAccount);
(*users).showUsers();
return 0;
}
...I get the desired output.
Thanks a lot.