use getline as argument in while loop

I want the user to be able to put data in to my vector by using this while-loop, and when the user enters 'end' the loop breaks.

1
2
3
4
5
6
7
8
9
10
11
12
    while ( getline(Value) )
    {
        // are we done?
        if ( Value == "end" )
        {
            // Yes
            break;
        }

        // add to vector
        Data.push_back ( Value );
    }


I did use while ( cin >> Value ) and it did work, but when the user hits space, the next word is put on an other place in my vector. I want to be able to put in for example: "James Bond", and still on the same line.

Why does not while ( getline(Value) ) do this?

I get error mesage:
error: no matching function for call to 'getline(std::string&)'
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
    while ( getline(std::cin, Value) )
    {
        // are we done?
        if ( Value == "end" )
        {
            // Yes
            break;
        }

        // add to vector
        Data.push_back ( Value );
    }

The function takes an istream object as an argument.

use getline as argument in while loop

In this case, getline() is actually a condition. The loop will break if the stream fails.
Last edited on
Thank you! How stupid of me.... =)
Topic archived. No new replies allowed.