I already know how to work a solution for this petty question in the conventional and "sane" way of comparing input with the integer 42, as suggested by @firstPerson and @csurfer.

But I was left goofified with the "that" solution in which '4' and '2' are compared separately as chars and then something weird happening to them, producing the right result. @firstPerson-- Yes the code is super ugly, but out of curiosity I still wanted to know how that chunk of code worked.

so basically when the user inputs a number , it gets saved into d,
and when the user input another number, it checks if that number is 2 and if the previously saved number was 4. if so then it quite, otherwise it continues.

Now as there are no more inputs to read in the buffer until the buffer gets newly filled by some other characters the cin.get() waits and hence the while loop waits.
As we have already seen the first input 10 has already been printed and d holds '\n' and waits to print it.

Assume our next input is 42 taken as '4' '2' '\n'

Loop4 : d='\n' ,c= (un-assigned) <same as the first loop condition>

c1) cin.get() therefore c='4'c2)c!='2' and d!='4'c3) prints d ,ie a new line and then d=c

Man....
Thankyou guys. Both firstPerson and csurfer were awesome in there explanation... The focused and step by step explanation by csurfer was damn cool. Simple enough to get a first grader sailing...