Jump to content
  • Advertisement
Sign in to follow this  
gabriel76

c++ question

This topic is 4388 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

i want to know if you can create a string and name it from a different string for example
string name;
cin>>name;
string (what name equals)[guess]*name[/guess]
thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Not exactly - like Evil Steve says, that's impossible.

What you can do is using an associative container that maps strings to other strings:


std::map<std::string,std::string> string_map;

string name;
string_map[(what name equals)] = name;
string_map[(what name equals)];

Share this post


Link to post
Share on other sites
i'm trying to make something like a phone book in a console window and i need it to be able to make a new string for a name and a number and then be able to check its number just by writing the name of the person tell me if this is possible

Share this post


Link to post
Share on other sites
Yes. But not at compile time. You're thinking is almost precisly backwards. You setup a datastructure to hold the information, then access it at runtime however you please.

Share this post


Link to post
Share on other sites
Nitage already answered your question.


map<string, string> phonebook;
phonebook["Bob"] = "555-555-1234";

cout << "Bob's phone number is " << phonebook["Bob"] << endl;

Share this post


Link to post
Share on other sites
tell me if i'm missing the point but i mean if you don't now the names at the begining you put every thing in later (names and numbers)

could i make it read all the files in a folder meaning everytime you put in a name it saves it as a .txt file or something and then you type in the name and it will write everything in that file (name and number)

Share this post


Link to post
Share on other sites
Quote:
Original post by gabriel76
could i make it read all the files in a folder meaning everytime you put in a name it saves it as a .txt file or something and then you type in the name and it will write everything in that file (name and number)


Yes. Though it would be more practical to have one .txt per 'phonebook'. The textfile could be laid out like so:

Peron's name
phone number
Other person's name
other phone number
Another person's name
another phone number

Ect. This requires only one file name (which is easier to do than to get all file names in a directory, which requires 3rd party libraries). Then you'd have a while loop that reads lines in until one of them is EOF (end of file, IYDK).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!