I am fairly new to C++ and am trying to write a program that can sort a set of names (in alphabetic order) that I input but I'd like to make it so I can input all the names at once; I have already coded the sorting and have tested with multiple names fine but right now I have to push enter after every name to signify a new one. I searched for a way to separate the input based off the spaces in the input but all I found was this which only separates the first 2 words/names:
int main(){
string input;
getline(cin, input);
string temp1;
string temp2;
for (int i = 0; i < input.length(); i++){
if (input[i] == ' ') {
temp1.append(input.substr(0, i));
temp2.append(input.substr(i + 1, input.length() - 1));
break;
}
}
cout << temp1 << endl;
cout << temp2 << endl;
}
I have played around with the parts that appear to separate the code and tried to make them repeat for every space but I can't get it to work. As I said, I'm fairly new to C++ so if anyone could please steer me in the right direction or suggest a better way to accomplish what I'm trying to do that would be great.
Thanks,
-Eric
---Edit---
Example Input:
William Charlie Sarah Peter Matt John
Example Output:
Charlie John Matt Peter Sarah William
(As I said, I already have the program to do the name ordering, I just need to know how to input all the names at once and have the program assign the first to temp1, second to temp2, third to temp3, etc.)