I'm working on a chatroom program, and I am trying to add a user to a chatroom map. My chatroom map is stored in my Server
class and it looks like this: map<Chatroom*,int> chatrooms
where int is the number of users in the chatroom. Also in my Server
class is a vector of all the users currently in the server:
vector<User*> current_users
. server.getUsers()
returns the current_users
and server.get_chatrooms()
returns the map chatrooms
. My function properly adds the user to the chatroom, however, it doesn't increment the number of users in the chatroom. I wrote a comment where the problem is.
Here is the function.
void Controller::add_user_to_chatroom(){
string username, chatroom_name;
User* user;
bool foundChat = false;
bool foundUser = false;
view.username_prompt();
cin >> username;
//this loops checks to see if user is on the server
for(auto x : server.get_users()){
if(x->getUsername() == username){
user = x;
foundUser = true;
break;
}
}
if(!foundUser){
cout << "No user found.\n" << endl;
}
else{
view.chatroom_name_prompt();
cin >> chatroom_name;
//adds user to chatroom, but doesn't increment the number
for(auto x : server.get_chatrooms()){
if(x.first->get_name() == chatroom_name){
x.first->add_user(user);
//line below doesn't work, tried x.second++;
server.get_chatrooms().at(x.first) += 1;
foundChat = true;
break;
}
}
if(!foundChat){
cout << "Chatroom not found.\n" << endl;
}
}
}
My output when there I print chatrooms looks like this:
Chatroom Name: Sports, Users: joey1212, , Num Users: 0
However, it should look like this:
Chatroom Name: Sports, Users: joey1212, , Num Users: 1
since there is one user in the chatroom.
Why is x.second
not updating? I've added several users to the same chatroom and num users never updates. Just in case it's needed, here are the other functions that were called from add_user_to_chatroom()
Here's Server::get_users()
vector<User*> Server::get_users(){
return users;
}
Here's Server::get_chatrooms()
map<Chatroom*, int> Server::get_chatrooms(){
return chatrooms;
}