I've got a function to split a string by spaces.
Here's the code:
vector<string> split(string text) {
vector<string> output;
string temp;
while (text.size() > 0) {
if (text[0] == ' ') {
output.push_back(temp);
temp = "";
text.erase(0, 1);
}
else {
temp += text[0];
text.erase(0, 1);
}
}
if (temp.size() > 0) {
output.push_back(temp);
}
return output;
}
int n = 0;
int main()
{
while (1) {
cout << "Main has looped" << endl << "Input:";
string input;
cin >> input;
vector<string> out = split(input);
cout << n << ":" << out[0] << endl;
n++;
}
return 1;
}
This should split input by spaces, and it seems to do that, except the function only returns one value at a time, and does so repeatedly until the output vector is empty:
Main has looped
Input:Split this text
0:Split
Main has looped
Input:1:this
Main has looped
Input:2:text
Main has looped
Input:
It also seems to be skipping the input, for some reason. I have no clue what's happening, please help!