Splitting input to substrings.

I have been having a problem my code recently. i wish to be able to take an input from the user as a string then split it into substrings deliminated by whitespace. i did some research and discovered the istringstream iss() function. i tried this function with the following test code:
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string t = "Hello world this is a test";
    istringstream iss(t);
    string input[7];
    int x = 0;
    do
    {
        string sub;
        iss >> input [x];
        x++;
    } while (iss);
    x = 0;
    while (x < 7){
          cout << input [x] << " ";
          x++;
          }
    system("PAUSE");
    return 0;
} 

and received the result
Hello
world
this
is
a
test

Press any key to continue...

however when i try similar code with user inputted strings:
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string t;
    cin >> t;
    istringstream iss(t);
    string input[7];
    int x = 0;
    do
    {
        string sub;
        iss >> input [x];
        x++;
    } while (iss);
    x = 0;
    while (x < 7){
          cout << input [x] << " " << endl;
          x++;
          }
    system("PAUSE");
    return 0;
}

i get the following as output
Hello





Press any key to continue...
Any help is greatly appreciated!
Last edited on
On line 9, cin>>t will stop reading at a whitespace character. Use std::getline(std::cin, t) instead.
THANK YOU SO MUCH! You have no idea how long this has taken me to figure out! is there a way to, for lack of a proper term, "thumb up" your account, so other users know how helpful you are? (Im new to this forum btw)
Topic archived. No new replies allowed.