Thanks for all the help! I had to get to sleep last night after my post. Today I have been studying your replies.
@ar2007:
after line 14, adding this:
stream.clear() ; |
@coder777:
Meden:I don't see why cString is empty. |
The reason is the missing whitespace after "dog". On line 13: Trying to read the whitespace leads to eof error. After that it does not accept io anymore. |
Could this be explained a bit more? I am using Visual Studio and walking through the debugger while Watching all the variables. I notice that after
stream << aString;
, stream's _Mystate goes from 0 to 1. Does this signify that (I am not sure how to put this.) eofbit has gone into an error state? If _Mystate might have been 2 or 3, would this signify error states of failbit or badbit respectively? If _Mystate does not apply to the state of the stream, is there something that I can Watch that does? Is it possible to see the state of the stream during debugging rather than writing periodic statements to check? My questions are partly based on information found here:
http://www.cplusplus.com/reference/ios/ios/bad/
Was coder777 saying that a stream needs whitespace to know that it is terminated? I wonder though if it should be preferable to avoid getting into these error states rather than setting them back to a good state. If the stream expected whitespace, would it not be better to give it whitespace?
@lastchance:
bString = stream.str(); stream.str(""); |
How is stream.str("") applied in this instance? I understand that it empties the stream without clearing error state. But what exactly is the rationale of why it is used and what issues it will solve?
@coder777:
Why do you want to use the stringstream? It is rather problematic to use it like this. |
My primary objective I suppose is to learn about stringstream, but I am also working on a palindrome math problem where I need to iterate over a sequence of palindromes. It is Project Euler problem number 4.
https://projecteuler.net/problem=4
The reason I am using stringstream is that it seems to be the most popular answer in how one might concatenate integers. I realize I could devise a mathematical method to convert a three digit number into a six digit palindrome, but with all of the extra variables and assignments necessary for that, I wonder if it might not be better to use a string method instead.
I wrote code for the iteration of a palindrome number sequence. I based it on kemort's double type palindrome and stole
std::stringstream().swap(convert);
from here:
https://stackoverflow.com/questions/20731/how-do-you-clear-a-stringstream-variable
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 30 31 32 33
|
#include <iostream>
#include <sstream>
#include <algorithm>
int main()
{
std::string palindrome;
int palindromeInt = 0;
std::stringstream convert;
for (int prefix = 987; true; prefix--) {
// swap convert with a default constructed stringstream
std::stringstream().swap(convert);
// convert = 987
convert << prefix;
// palindrome = "987"
palindrome = convert.str();
// palindrome = "789"
std::reverse(palindrome.begin(), palindrome.end());
// convert = 987789
convert << palindrome;
// palindromeInt = 987789
convert >> palindromeInt;
// do math on palindromeInt and return if math has desired answer;
}
}
|
How is my code? Could it be improved?