while loop doesn't work!

I am a newcomer to c++ , and i have a very strange problem.I know i should use ^z to terminate a while loop such as while( cin >> string ) under windows system, and if there is only one such while loop , it works well, but if i have two, the second one won't work, why?
1
2
3
4
string str;
while( cin >> str ) cout << str << endl;
cout << "Another: " << endl;
while( cin >> str ) cout << str << endl;

The second loop won't begin. It exits immediately after the Another: was printed out, why? I am so confused. I would appreciate it very much if you help me out!
Thanks!
ps: I use Mingw under windowsXP.
Last edited on
this happens, because "^z" will make the corresponding stream into fail-state (check with cin.fail() yourself)...

you need to clear the fail-flag with cin.clear();


for more information you might read http://www.learncpp.com/cpp-tutorial/135-stream-states-and-input-validation/ or whole chapter 13... (it´s interesting;) )
well, thanks! This is gonna help a lot!
Topic archived. No new replies allowed.