The following code splits the provided string/line down to the characters. Why does the loop repeats that last string twice? How to fix it?
#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);
do
{
iss >> sub;
cout << sub << endl;
vector<char> v(sub.begin(), sub.end());
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
} while (iss);
return 0;
}
Input:
hello world
Desired output
hello
h
e
l
l
o
world
w
o
r
l
d
Actual output:
hello
h
e
l
l
o
world
w
o
r
l
d
world
w
o
r
l
d
I have removed elements which are unrelated to the problem as much as possible