getline compile error: no matching function for call to 'getline'

I'm working my way through "Jumping Into C++." I was working on Ivor Horton's book, but felt I wasn't retaining anything. I was several chapter's in and didn't feel like I could repeat anything I had supposedly learned.

So I'm working on one of the samples in the book and it has a getline function. I've typed the code exactly as in the book and get the following error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o username_password.o "..\\username_password.cpp" 
..\username_password.cpp: In function 'int main()':
..\username_password.cpp:18:29: error: no matching function for call to 'getline(std::istream&, std::string&, const char [2])'
  getline(cin, username, "\n");
                             ^
..\username_password.cpp:18:29: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:53:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from ..\username_password.cpp:7:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.tcc:1068:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
     getline(basic_istream<_CharT, _Traits>& __in,
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.tcc:1068:5: note:   template argument deduction/substitution failed:
..\username_password.cpp:18:29: note:   deduced conflicting types for parameter '_CharT' ('char' and 'const char*')
  getline(cin, username, "\n");
                             ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from ..\username_password.cpp:7:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2793:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
     getline(basic_istream<_CharT, _Traits>& __is,
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:2793:5: note:   template argument deduction/substitution failed:
..\username_password.cpp:18:29: note:   candidate expects 2 arguments, 3 provided
  getline(cin, username, "\n");                        ^


Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string username;
	string password;

	cout << "Enter your username: " << "\n";
	getline(cin, username, "\n");

	cout << "Enter your password\n ";

	if(username == "rppt" && password == "xyzzy")
	{
		cout << "Access granted.\n";
	}
	else
	{
		cout << "Access denied.";
		return 0;
	}

	return 0;
}


I did search the error, and looked at several other posts, both here and stackexchange, but none of the other's issues fit mine exactly.
Last edited on
The third parameter to getline is a character not a string.
@Yanson, thank you. Changed to single quotes and compiles flawlessly.
Topic archived. No new replies allowed.