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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With