0

I am trying to take input from command prompt and put every input separated by white-space or tab to vector until user presses "enter". I am not able to do do. here is my code

template <typename T> 
vector <T> process_input_stream() {

    T value;
    vector <T> vect;

    string line;
    //read input and populate into vect until return key is pressed
    while (getline(cin, line, '\n')){
        cin >> value;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
        vect.push_back(value);
    }

    return vect;
}

now the problem i am hitting my head to, is, when entering input, input is screen is still asking for more input even after pressing enter key.

Amaresh Kumar
  • 586
  • 1
  • 8
  • 20

1 Answers1

3

The line variable contains the whole line, except the linefeed.

To parse each line, you could replace your while loop with:

while (getline(cin, line))
{
    istringstream iss(line);
    while (iss >> value)
    {
        vect.push_back(value);
    }
}
Sid S
  • 6,037
  • 2
  • 18
  • 24