How to parse string variable using istringstream to store words in a vector?

The program takes in strings from user input into words separated by white space,comma, and newline so that each word can be added to a vector as an individual element (the user terminates the program by initiating EOF via CTRL + D). I want the vector to be emptied when the user types in the dot character "." on a line by itself.

The following code with the commented part is the troubles I'm having. I want the big while loop to take inputs one line at a time so I can compare that to "." to check if "." is by itself. I seem to have too many while loops and so I can't initiate EOF (CTRL + D) because of one of them--how can I simplify the mess I have while being able to initiate EOF, process the string into elements for a vector and still be able to terminate when the user types "," on a line by itself?

Thank you and much appreciated--have been working on this simple program for hours.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
   string input;
   vector <string> hand;
   istringstream sso;

    cout << "Enter your string: " << endl;

    while (getline(cin,input)){
        
        //if (input != ","){
        while (input != "."){
            sso.str(input);
            while(getline(sso, input, ',')) {
                hand.push_back(input);
            }  
        sso.clear();   
       }
      // else{
      //     cout << "seems to be working" << endl;
       // }  
    }

    cout << hand.at(0) << endl;
    cout << hand.at(1) << endl;
    cout << hand.at(2) << endl;
    cout << hand.at(3) << endl;

    return 0;

}
Last edited on
1
2
        //if (input != ","){
        while (input != "."){
explain


Also http://www.cplusplus.com/faq/sequences/strings/split/
That is to check if the line from user input is just the dot character by itself, which signals the vector to be cleared so I can use it for the next input. I commented the "if" part because I was trying to find a way to reduce the while loops since I'm currently stuck in one of them when I run the program--EOF can't even be initiated by the user via CTRL + D on Linux or CTRL + Z on Windows.
> That is to check if the line
yet you wrote while
the loop 11--17 does not modify `input', so it is an infinite loop
Topic archived. No new replies allowed.