I have a while loop that uses stringstream/getline() to parse a string, however I’m having trouble manipulating the results from the loop. The loop splits the string up into 3 parts and puts each word in the variable “word” for that cycle of the loop. However, How can I then store each part in a variable or array so that I can use it outside the while loop?
The loop
string word;
stringstream stream(cmdArgs.c_str());
while( getline(stream, word, ' ') )
// Manipulate results
The variable “cmdArgs” is the string.
Using a vector lets you break the string into words and store each word individually, no matter how many:
if you are confidant that there are exactly 3 words you could also just use a plain array:
but you would overflow that array if the incoming string was longer than you expect.