Searching text-document for a specific sentence

Pages: 12
Yes you were very very helpful, can't thank you enough. This will keep me busy for quite some time before I have to overcome my next programming hurdle :)
I am going to attempt to take this example and do something a bit different.
In addition to my currentLocation i have a lastLocation, in this manner I want to present a different description if reaching location HOME from FARM for example. So my currentLocation is farm, and lastLocation is house, so something along the lines of
1
2
3
4
last current description

FARM HOUSE |You walk from the farm to the house|
HOUSE FARM |You walk from the house to the farm|

I feel like this will create a bit more depth to the game, instead of presenting the same message everytime you reach a location, it will present a different description if you approach from the south or north, hard part is coding it as always! lol.
Last edited on
Ok,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void map::displayAreaDescription()
{
std::map<std::string, std::string> sentences;
     
     std::ifstream load_description("game_text_files/story.txt");
     while ( load_description.is_open() )
     {
           std::string line;
           
           while ( std::getline(load_description, line) )
           {
                 std::string location = std::string(line, 0, line.find_first_of(" "));
                 std::size_t first = line.find_first_of("|");
                 std::size_t second = line.find_first_of("|", first + 1);
                 std::string sentence = std::string(line, first + 1, (second - first) - 1);
                 sentences[location] = sentence;
           }
                 load_description.close();
     }
           
           std::string which; //next 3 lines are new, and in my text-file I have HOMEFARM, shouldn't this work if my current location is HOME and last FARM?
           which = currentLocation;
           which = which + lastLocation;
     if (sentences.find(which) != sentences.end())
        {
           std::cout << sentences[which];
        }

}
Yup, I GOT IT :D
Yup that should work fine from what I can see you can also eliminate a few lines also so it looks like this.

std::string which = currentLocation + lastLocation;
Ah, yeah, don't know why I did that!
Topic archived. No new replies allowed.
Pages: 12