'no matching function to call for "getline"
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.
Solution 1:[1]
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);
Solution 2:[2]
add this line work for me
#include <fstream>
Solution 3:[3]
If you change it to a char array you can use:
cin.getline(my_name, val_max);
Just make sure your buffer (val_max) is large enough
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | weigan |
| Solution 3 | KnowOne |
