You are mixing things up quite thoroughly. You need to be careful how you use things.
Firstly, the value of
c is always
random, since you never assign it any value. The fact that it was null once then something else later is just pure happenstance. It could be anything.
Next, watch how you use your streams. EOF will only be signaled after you have made at least one attempt to read past it.
So, using the first code as example, if I type "hello" then it will read 'h', 'e', 'l', 'l', and 'o' without any problem, but EOF was still not discovered. The next time through the loop you try to read it, and regardless of whether or not you have hit it you print
c again, which still has an 'o' from the last loop. Then you loop again, discover the EOF, and quit.
When using stringstreams it is usually a good idea to generate them as temporaries when you need them, then destroy them when you are done with them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
char c;
while (true)
{
cout << "Please enter a string. Ctrl-C to quit.\n";
string s;
getline( cin, s );
stringstream ss( s ); // note 1
for (int i = 0; ss >> c; i++) // note 2
{
cout << "Letter: " << i << " is '" << c << "'\n";
}
}
return 0;
}
|
You'll notice that whitespace is ignored when you
cin >> c;
In the above example, the stringstream (and the string) are local to the loop, so they are both created and destroyed once per loop.
[edit] Oh yeah, it isn't particularly obvious, but the inner loop is basically a
while (ss >> c)
. See why this works here:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.4
Hope this helps.