What's actually happening is something a bit different
cin >> ch1;
cin >> int1;
ch1 is a char so it holds 1 character. When you are typing in the character to go into this variable, you hit enter. Enter is a character which remains in the input stream and is processed by the cin >>int1; call which interprets it as "someone has pressed enter" so it skips the statement entirely without letting you enter an integer. The answer above about int1 being an uninitialized value is pretty much correct (though I believe it's more that the compiler sets aside an "empty" portion of the memory which could have any state and not so much that it assigns the memory an arbitrary value -- I may be inaccurate on that point).
P.S. You can eliminate that extra newline from the stream by placing a cin.ignore() between the 2 lines above.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
ch1 is a char so it holds 1 character. When you are typing in the character to go into this variable, you hit enter. Enter is a character which remains in the input stream and is processed by the cin >>int1; call which interprets it as "someone has pressed enter" so it skips the statement entirely without letting you enter an integer.
The extraction operator (>>) will skip over the enter (newline) as that's whitespace.
If you enter the input as :
a
z
or as:
a z
or as
az
it will all be handled alike. The 'a' will be stored in the char variable, and cin will attempt to read the z and store it to the integer variable, which fails. The z will still be in the input stream, but the stream is in a bad state.
Why the OP gets various values displayed for the integer is compiler dependent action, as I showed in the response to the earlier post dealing with this same issue. In release version code, uninitialized automatic variables should hold random values, whatever the bits happen to be in the assigned memory. In "debug" code (which is the default in VC++), variables do get assigned a default value. In VC++ this value is 0xcccccccc for an int.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Well vmanes, that was a well-deserved semi truck driven through that garage sized hole in my knowledge :)
I do normally test these things. I went and tested to see what I was really thinking of and I guess that I made too much of a generalization from the stream problems resulting from combining cin and cin.getline. I had also misunderstood that the OP had been feeding 2 chars into the cin. Apologies to the OP.
Also thanks for the heads up on the Debug vs. Release behavior.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
That was more like a Volkswagen than a semi.
We're here to learn and to share.
I'm pretty sure I've given answers to things other than the stated problem myself. As long as nobody gets hurt....
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228