Mar 23, 2012 at 1:56am Mar 23, 2012 at 1:56am UTC
Well, I tried it on my system and as you said, the o/p was wrong. So I too traced your program and turns out the code is right, but this printing part was the culprit
cout << stack.pop() << " " << stack.pop() << endl;
and so, I modified it too
1 2 3
cout << stack.pop() << " " ;
cout << stack.pop() << " " ;
And now the program runs just fine. Now as for why it happened, I wrote a small program quickly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
const char * Foo()
{
static bool FirstTime = true ;
if (FirstTime)
{
FirstTime = false ;
return "This is First Time\n" ;
}
else
{
return "This is second time\n" ;
}
}
int main()
{
cout << Foo() <<Foo() << endl;
return 0;
}
Expecting its output to be
This is first time
This is second time
But instead what it gave is, Bingo!
This is second time
This is first time
Turns out, the second function (in the bold) gets executed first. Wow!
Last edited on Mar 23, 2012 at 2:00am Mar 23, 2012 at 2:00am UTC
Mar 23, 2012 at 4:04am Mar 23, 2012 at 4:04am UTC
Whoa. That's weird... I don't recall ever running into that before :p