Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no matching function to call for "getline"

Tags:

c++

I'm new to c++ programming. In a tutorial, the author mentioned "cin" will break if it reads a space in a string. If you want the program to read in an entire line of string with spaces, you should use the function "getline".

However, I couldn't make it work.

Here are my codes:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string my_name;

    cout << "Please enter your name: ";
    getline(cin, my_name, "\n");

    cout << "My name is " << my_name << " .";
}

The IDE I am using is Xcode. The error message is "No matching function to call to 'getline'".

I have searched for similar issues but it seems to me none of the solutions apply to my problem. Maybe I am missing some knowledge? Thank you.

like image 948
Noah Avatar asked Aug 10 '15 22:08

Noah


1 Answers

The complete error would tell you why your third parameter is of the wrong type.

It should be a character type, not a null-terminated string of characters.

getline(cin, my_name, '\n');

Edit: And, '\n' specifically is the assumed delimiter in another form of getline:

getline(cin, my_name);
like image 188
Drew Dormann Avatar answered Sep 28 '22 09:09

Drew Dormann