Detect Players in Vector C++

Hello! I've learned a lot in C++ over the past few weeks but I've hit a wall. Here is how my program works:
A player registers then logs in. The program saves the players names and passwords in a file. I have a player class with two vectors, one for the name and one for the password of each player. Sure, it detects whether that player already exists by comparing the players name and pass entered on login by the one in the file, of course.. But what if a player then types 'stats'? How can C++ possibly know the vector index to retrieve information for that particular player? No one seems to understand my question elsewhere and it is frustrating to know this isn't possible for me at this point.

Player 1 logs in. Vector pushes back player name and password.
Player 2 logs in. Vector pushes back player name and password.

Player 1 types stats..

1
2
Player newplayer;
std::cout << "Name: " << newplayer.name[index] << " || Password: " << newplayer.password[index] << std::endl;


C++: Should I show you player 1 stats at index 0 or player 2 stats at index 1? Which player are you?!

How can I actually make the program know which player is using the console when they've logged in?
Last edited on
newplayer.name[index]
This is a strange way to design a data structure. Usually you'd have something more like players[index].name.

Dumb example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::cout << "Name: ";
auto name = input();
std::cout << "Password: ";
auto password = input();
int found = -1;
int index = 0;
for (const auto &player : known_players){
	if (player.name == name && player.password == password){
		found = index;
		break;
	}
	index++;
}
if (index < 0){
	std::cout << "Incorrect login\n";
	//handle this
}

while (true){
	auto command = input();
	if (command == "stats"){
		std::cout << "Name: " << known_players[found].name << " || Password: " << known_players[found].password << std::endl;
	}
}
I like your layout. What if the second player logs in afterwards, then the first player types 'stats'?
The index int will then be applied to the second player instead, showing the second players stats to the first.
If there are two players logged in, and someone types in "stats", how would you know which one typed it in?
Clearly, you can't have more users logged in than you have consoles, because you have no other way to tell them apart.
How do MUD games work then? Those programs remember the players details and differentiate between them.
Well, I've never played one, but if we're talking about a multiplayer game, that usually would be implemented with multiple computers talking to each other through a network, therefore you'd have multiple consoles, not just one.
Topic archived. No new replies allowed.