Jump to content
  • Advertisement
Sign in to follow this  
Alex_H

Assigning(nameing?) a string/char array from a user input

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Bit of noob c++ Question. I am in the process of creating a internet chat program. I have done the main part of it, allowing clients to chat with one another, even PM each other. I have a list of names of the people connected, and now I am in the process of creating rooms so that groups of people can chat privatly. And it is here I am coming stuck. I currently have list of Created rooms, in the form of a string on the server, I can send this to clients which then can split it up and display the list of names, the same goes for the list of client names. Currently when a client wants to create a room he sends a message to the server with the name of the rooms name, this is then added to the RoomList in the first empty space. What I am wanting to do, is to take this "room name" and create a new string using its name....., then I can then add the clients name, and any other clients who joins to that string. I know that might be a confusing explination so here is the best pseudo code I can make to try and help. C1 = Client 1 C2 = Client 2 S = Server. :D C1 -> Create Room Called "Fred's Room" S ----- Creates a string called "Fred's Room" and adds C1 to Room1[Position 1] C2 -> Join Room1 S ----- Adds C2 to string "Fred's Room[Position 2] So basically can I create a string naming it from an user input? If I knew how to include the code, like I have seen on other posts I would so you could get a clearer idea of what I want I would. Could someone also help me how to do this?

Share this post


Link to post
Share on other sites
Advertisement
I don't think you can do that.

An idea would be to have the roomlist in an array, and the users in anothr 2d array.

eg

string roomlist[10]; //10 = max rooms, 100 = max users
string users[10,100];

so you have a room called testing say and users a, b, c,

roomlist[0] = "testing";
users[0,0] = "a";
users[0,1] = "b"; // 0 refers to room 0, 1 refers to user 1
users[0,2] = "c";

etc...

I bet at leas two people have posted actual solutions before me. lol
edit1: wow I was actually first
edit2: I don't use strings, so don't take the definitions literally, its more like pseudo.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It sounds like you want to set the name of a variable at runtime. This is not possible.

If you've ever used STL, a possibility is to use a std::map. This is basically an array that can use any type for indices. A possible declaration for your rooms:

std::map > rooms;

This allows you to use strings to index the array (the first template parameter), so you can access an element with:

rooms["Freds Room"]

The second parameter in the template list is a vector (essentially, another type of STL array) that is also of type string. This would end up being the list of people currently in the room. If you're familiar with STL, or look into it a bit, this should more or less be clear.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Declaration didn't come through correctly. It should be:

std::map<std::string, std::vector<std::string> > rooms;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!