The problem is that converting to bool istream only returns false if bit is set or bit is set, but not if the stream is empty. For example, a failed set is set after you try to extract a string from an empty istream. This is why your loop works again: when istream is empty, failbit is not set, only after no line can be extracted in an additional iteration, the failure bit is set and the loop ends. The solution may be to use:
#include <iostream> #include <vector> #include <sstream> #include <string> using namespace std; int main() { string main, sub; cout << "Enter string: "; getline(cin, main); istringstream iss(main); while(iss >> sub) { cout << sub << endl; vector<char> v(sub.begin(), sub.end()); for(int i = 0; i < v.size(); i++) { cout << v[i] << endl; } } return 0; }
source share