fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. {
  8. std::istringstream stm( "one two three four bad " ) ;
  9. std::string word ;
  10. int n = 0 ;
  11. while( !stm.eof() ) // bad
  12. {
  13. stm >> word ;
  14. std::cout << ++n << ". " << word << '\n' ;
  15. }
  16. }
  17.  
  18. std::cout << "----------------------\n" ;
  19.  
  20. {
  21. std::istringstream stm( "one two three four good " ) ;
  22. std::string word ;
  23. int n = 0 ;
  24. while( stm >> word ) // good
  25. std::cout << ++n << ". " << word << '\n' ;
  26. }
  27. }
  28.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1. one
2. two
3. three
4. four
5. bad
6. bad
----------------------
1. one
2. two
3. three
4. four
5. good