How to add things to a string

For example, in a RPG like Skyrim, you can have a bunch of quests added to your quest log. How would you do this in C++? When you pick up a new quest, or whatever, how would it be written to be added to a list in the log and how can you access that log at any time? Thanks
1
2
3
4
5
6
stringstream stm;
stm<</*anything*/ //ading into string
string a=stm.str() // getting the string
//or..
/*anything*/ x;
stm>>x //extracting formated data from the log 
Viliml is overcomplicationg it.

1
2
3
string str, str2;
cin>> str2;
str = (str + str2);


this just asks for input, then appends it to str. You can add characters to the string as well.

Also, you can get certain characters out of a string by treating it like an array or vector:

1
2
str2 = "abcdefg";
str = (str[0] + str[1] + str[2]);


now, str = "abc".

You can do this easier by using a for loop to handle larger quantities of characters:

1
2
3
4
for(unsigned int x = 0; x < 3; x++)
{
    str = (str + str2[x]);
}


that code does the same thing as the first. You can also use stringname.size() to get the size of a string. Note that the size returned by it is 1 more than the actual amount of characters in the string. So, when making a for statement, always use the < symbol so that we dont call a character that doesn't exist.
Last edited on
There's probably more to a quest than a string. In fact, a quest is probably an object of it's own. For simplicity:
1
2
3
4
5
6
7
8
9
10
11
class Quest
{
private:
  string name;
  bool active;
  bool complete;
public:
  Quest(string name) : name(name), active(false), complete(false) {}
};

std::list<Quest> quests;


I would guess there is a lot more to it than that. Perhaps the only things that are stored are file locations. I would guess that there would be functions that look like
1
2
3
4
5
Quest::Activate()
{
  active = true;
  add that NPC there...
}
.

But for this example, every quest would be added to the std::list when you start the game. When a game is loaded, quests are marked as active/complete. I would sort the active quests to the beginning, and the complete ones to the end (leaving the rest in the middle).

Then getting the UI to display the name of the quest would be a matter of going through and loading the quest names until a non-active quest is found.
Last edited on
Topic archived. No new replies allowed.