Alright, I cannot get the getline function to work. I have iostream included, using namespace std, and here is the syntax I have down: cin.getline(playerOne, 21); with playerOne declared previously as a string, and I want its max input to be 20 characters.
What's going on here? It's not working in Dev-C++, and I can't figure out why.

11 Years
Discussion Span
Last Post by WolfPack

there are two formas of function getline -- one for c-style character arrays and the other for std::string objects. You are trying to use the incorrect one. Here's what you need:


According to http://www.cplusplus.com/ref/iostream/istream/getline.html, the two acceptable forms of cin.getline() are:

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

Both versions expect a char pointer as the first argument.. not a string class object. Luckily, string class objects contain a method that will return a char pointer. Try this:

cin.getline(playerOne.c_str(), 21);

Edit: Ancient Dragon's post is the preferred method actually.


Try this:

cin.getline(playerOne.c_str(), 21);

This will not work. c_str() returns a constant pointer to the character array. You can't change it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.