This is simplification of my classes (a "gang-management" game logic):
class Player
{
private:
string nickname, password;
Gang my_gang; //this player's gang class instance
public:
//generic functions...
}
//PlayerContainer is a database of all possible players, each player has a specific gang assigned
class PlayerContainer
{
private:
list<Player> player_list;
public:
void add_player();
void remove_player();
// and many more .....
void login();
void sign_up();
}
Now I wanted to test the login/sign up functionality in the console. In main, the program first asks a user for a login, or for a registration (new gang creation).
Following is the login() function definition (only the parts that I thought would be necessary):
//if login() returns false, the rest of the program won't continue.
//If true is returned, the program forwards to the menu-part.
bool PlayerContainer::login()
{
string nick, pass;
cout << endl << "Enter your nickname: ";
cin >> nick;
cout << "Enter your password: ";
cin >> pass;
list<Player>::iterator it;
for (it = player_list.begin(); it != player_list.end(); ++it)
{
if (it->get_nickname() == nick && it->get_password() == pass)
{
return true;
}
else
{
//breaks the loop and lets the function return false;
}
}
return false;
}
Now the sign_up() function:
void PlayerContainer::sign_up()
{
string nick, pass, gang_name;
cout << endl << "Enter your nickname: ";
cin >> nick;
cout << "Enter your password: ";
cin >> pass;
list<Player>::iterator it;
/*Check whether there are no known players*/
for (it = player_list.begin(); it != player_list.end(); ++it)
{
if (it->get_nickname() == nick)
{
cout << endl << "ERROR: Player [" << it->get_nickname() << "] already exists!";
return;
}
}
cout << "\tCreate your gang" << endl << "Enter name of your gang: ";
cin >> gang_name;
Gang* g = new Gang(gang_name);
Player* tmpp = new Player(nick, pass, *g);
player_list.push_back(*tmpp); //store the temp player in the player list
cout << endl << "Gang created successfully";
}
EXPECTATIONS:
- after signing up, the user "account" data should be stored in
list<Player> player_list.
PROBLEM:
- when trying signing up after logging in, the login function won't evaluate true (won't let me in).
From what I tried by observing the code behaviour, it seems it's never really saved in player_list. It must have "slipped through". I'm blaming this part of code:
Player* tmpp = new Player(nick, pass, *g);
player_list.push_back(*tmpp); //store the temp player in the player list
I'd be very thankful for any insights!