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.

Recommended Answers

All 3 Replies

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:

getline(cin,playerOne);

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.