Subsequent call to cin.get() does not work.

I wrote this as a very, very small test of how istream's member functions work:

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

using namespace std;

void main()
{
	char me;
	cin.get(me);
	cout << endl << me;
	cin.get();
}


Of course it works, reading whatever character you typed and echoing it. (Only after you press Enter though.)

My expectation is that the final line, cin.get(), should block until you press Enter. But it seems to skip and end the program. Any further calls to an overload of cin.get() WILL in fact block. But that first one doesn't. And I have no idea how to Google this one. What happens, please and thanks?
It's because it still has the \n in the buffer, which is the delimiting value. If you're reading entire lines of text, use cin.ignore() to flush the buffer out, and cin.clear() to reset the stream state.

http://www.cplusplus.com/reference/iostream/istream/ignore/
http://www.cplusplus.com/reference/iostream/ios/clear/
or u can do this.

use " cin>>me; " command to get a char input. if u want a line input then the initialisation changes to char a[80] here i have assigned 80 as the no. of characters including spaces that can be stored in the "char me".
then use cin.getline(me,80) to get the line input.

cin.getline gets the line. in the braces (me,80), me specifeis the character where the input is to be saved and 80, the no. of characters that it should take in it.

Remember : that the number u give in initialisation in the braces (here 80) should be the same as the no. entered in cin.getline. As we dont know how many chars the user can enter!

Hope i helped. I tried my best. If any quest.s pls just ask!!
cin >> me isn't really a char input. If used on a string, it gets a string until the first whitespace. Char implies a single character
Oh, \n is the delimiter. That makes sense.

Thank you, everyone, for taking the time to answer my simple newbie question. I appreciate it.
Topic archived. No new replies allowed.