Why does getline allow an int

According to your ref getline, 'Extracts characters from is and stores them into str '

So why can I say,
1
2
3
4
int someInt;
cout << "Enter value";
getline(cin, someInt);
cout << someInt++;


Should I not get an error saying, cannot convert std:string to integer or something?

It's an implicit conversion of string to int. Try writing it as:

1
2
3
4
int someInt;
cout << "Enter value";
getline(cin, someInt);
cout << ++someInt;


Look at the results when you type in, say, 4. Then look at it when you type in the letter y.
It shouldn't work, unless you have overloaded getline to work with integers.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int someInt;
	cout << "Enter value";
	getline(cin, someInt); // error: no matching function for call to ‘getline(std::istream&, int&)’
	cout << someInt++;
}
Hold on, my mistake, it does give an error, I was mixing something up.

But now you are saying it does work?

So I am confused again.
No, no. I was talking out of my ass. Ignore me, listen to Peter.
Topic archived. No new replies allowed.