0

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.)

Eric
  • 109
  • 13
  • 1
    possible duplicate of [How to split a string in C++?](http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c) – mostruash Sep 10 '14 at 22:16
  • Answer: http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – Protopopulus Sep 10 '14 at 22:18
  • Is there any way to tweak the code I already have? – Eric Sep 10 '14 at 22:24
  • Yes, but it depends on what you want to do with them; whether create a std::vector out of the words, or print them, etc... – Sam Varshavchik Sep 10 '14 at 23:46
  • Please add an example input and example output to your post. Don't create a comment, but edit your post. – Thomas Matthews Sep 11 '14 at 01:09
  • I just want to assign them to the string value temp1, temp2, temp3, etc. and then the program does the rest from there. – Eric Sep 11 '14 at 01:09
  • Do you need to create separate variables for each value or could you use a *container*, such as `std::vector` or `std::list` or an array? – Thomas Matthews Sep 11 '14 at 01:10
  • The way I set up my program is to process them as separate variables, so that would be the best if it's not too hard. – Eric Sep 11 '14 at 01:14
  • There will never be more than 15 to 20 names, it's more of a proof-of-concept for now, if that simplifies it at all. – Eric Sep 11 '14 at 01:17

1 Answers1

0
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

vector<string> separate_string(const string& input)
{
    istringstream input_stream(input);
    string buffer;
    vector<string> separated;

    while (input_stream >> buffer)
    {
        separated.push_back(buffer);
    }

    return separated;
}

int main()
{
    string test_string = "William Charlie Sarah Peter Matt John";
    auto names = separate_string(test_string);
    sort(begin(names),end(names));
    for (const auto& s : names)
        cout << s << endl;
}

Explanation:

The object input_stream of type std::istringstream takes a string on contruction. Then, you can use it like cin. Using extraction operator >>, the string is already separated by space characters.

The while-loop stops when it reaches the end of the input string.

The separated strings are stored in a vector for string.

The main is just a test program. You may write your own to complete your mission.

  • Thanks, this works great! I only have one last question which I feel really stupid asking because I'm sure it's really obvious but instead of directly defining test_string as "William Charlie etc." I tried to add "string test_string;" and "cin >> test_string;" right below instead and then it only outputs the first name I input in cmd. Where am I supposed to add the "cin" because obviously I'm doing something wrong. – Eric Sep 11 '14 at 20:14
  • @user3052738 Look at your own question. Can you find `getline(cin, input)`? That's what you want. –  Sep 12 '14 at 01:28
  • Where do I add that though? I tried adding where it was in my original code but it says none of the overloads could convert all the argument types. – Eric Sep 12 '14 at 01:50
  • Ok, i got it, sorry about that I'm really just getting to know c++. Thanks a bunch! – Eric Sep 12 '14 at 01:55