tags:

views:

263

answers:

2

Okay, I was writing a simple C++ function to combine cin'd strings. I'm working on Linux at the moment, so I don't have the luxury of a simple "getline(cin, input)" command. Here's the code so far:

string getLine()          
{
    string dummy;          
    string retvalue;          
    do
    {
        cin << dummy;
        retvalue += dummy;
    } while           
    return retvalue;          
}

What I want to know is this: is the prompt actually asking the user for input, or is it still reading from the buffer that was left over because of a space?

+6  A: 

I'm working on Linux at the moment, so I don't have the luxury of a simple "getline(cin, input)" command.

What's Linux got to do with it? getline is standard C++, except it's spelled cin.getline(input, size[, delimiter]).

Edit: Not deleting this because it's a useful reference, but AraK's post illustrating std::getline should be preferred for people who want a std::string. istream's getline works on a char * instead.

hobbs
cin.getline(...) is not for strings, it is for char*, good answer though :)
AraK
Does it show that I'm a C programmer who last did C++ ages ago? :)
hobbs
+8  A: 

There is a getline defined for strings:

std::string line;
std::getline(std::cin, line);
AraK