Supposing stringstream contains James is 4 , I can write something like getline (stream, stringjames, ' ') to get the individual words, but is there any way to know that I’ve hit the end of the line?
Bonus Question!
Case 1: James is 4
Case 2: James is four
If I were iterating through the words in a stringstream, and I expect to receive an int val of 4, but instead I received a string, what would be the best way to check this?
You check the return value to see if it evaluates true or false:
As for the “bonus question” you can also do the same thing when extracting
ints and things from streams. The return value ofoperator>>will evaluate totrueif the read was successful, andfalseif there was an error (such as there being letters instead of numbers):