Input skipping in simple program!

Hey guys so I'm just going through my programming book and I'm doing some exercises on functions. I have a function here that asks for the input of a character and feeds the value of the character back to main. For whatever reason, in the console, the input is skipped and the value of z is changed to
' '. I know I can put cin.ignore(); before the cin.get(); and it will work but I want to know why it works with cin,ignore()' and why it doesn't work without it!!

Any help would be really appreciated guys!

1
2
3
4
5
6
7
void getChar(char& z)
{
	cout << "The value of z currently is: " << z << endl;
	cout << "Please enter the new value of z.. " << endl;;
	cin.get(z);
	cout << "The new value of z is: " << z << endl;
}
program outputs: "The value of z currently is: "
You enter: 5 <ENTER>
In input buffer: "5\n"
formatted input reads 5 and leaves "\n" in input buffer.
cin.get(z); is unformatted input function it consumes whichever character is first in input buffer. There already is character so it reads it, no input from user nessesary.
Topic archived. No new replies allowed.