| ProbePLayer (22) | |||
|
What is wrong with this code? Suppose 1. tempString contains the following:"/454/665/4/"2. i is always the position of the first integer after "/"3. tempBuf points to the stringbuf of tempStream4. tempVar is int
I put this in a loop(if necessary, I'll put the rest of the code here also), and every time it should have different result(the next string of numbers should be assigned to tempVar), but it doesn't. I expect this: 454 454 665 665 4 4 (without the space, I put it for clearness) but I get this instead: 454 454 665 454 4 454 So what is wrong with the code? Or maybe I am expecting something wrong... Thanks in advance, and also, I have posted this in another thread, but didn't get an answer(although got answers for lots of other things, thanks shacktar, but I am in haste). Peace \/ | |||
|
|
|||
| shacktar (1143) | ||||||||
You might have to clear the tempStream after calling tempStream>>tempVar;.i.e.
I've found that you don't need to do that if you append a space after each number you input to the stringstream. The following doesn't work (it prints "42 42" when I expect "42 54"):
although the following works (note the spaces after the inputted numbers):
this is because stringstreams expect a space as a delimiter. If you just give it a number (without a space afterwards), some error flags will be set and you won't be able to continue until you call .clear();
You're welcome :) | ||||||||
|
Last edited on
|
||||||||
| ProbePLayer (22) | |
| Awesome, really needed this(deadline coming soon :]). And although I am getting quite boring, THANKS again \o/! (no harm in gratefulness, right?) | |
|
|
|
| ProbePLayer (22) | ||
btw,
is this something you learnt from books or from experience? cause it wasn't mentioned in the Reference page. Or maybe I missed it... | ||
|
|
||
| shacktar (1143) | |
|
I found that it was implied by examples such as this one: http://www.cplusplus.com/reference/iostream/stringstream/stringstream/ Although, it should be something that is more clearly stated. Unfortunately, I don't think there's a way to change the delimiter character. You're welcome again :) | |
|
Last edited on
|
|