0

I have the following code:

int main() 
{   
    int i = 0;
    cout << i;    //why is i not printed even though it is before the exception?  
    int j = 1 / i;   //divide by 0 
    j++;
    cout << i << j;
    return 0;
}

Why is i not printed? It should be printed, because it is before the exception occurs.

But nothing is getting printed, I just get the exception.

Any ideas?

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • [A division by 0 does not throw an exception](http://stackoverflow.com/questions/6121623/catching-exception-divide-by-zero). So it is quite possible the program could run to completion, and you would see the output. – PaulMcKenzie Oct 09 '16 at 07:56

2 Answers2

3

That's probably because the stream isn't flushed. On some platforms, it is flushed after every output, but on others, it is not.

So, if you flush it you'll get 0 as output:

cout << i << flush; // 'flush' flushes the stream = displays everything immediately
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
-1

I think you can refer to this function puts(s), you can write this sample:

int main()
{
    char a[20];
    int b = 1;
    while(b>10)
    {
        gets(a);
        puts(a);
        b++;
    }
    return 1;
}

you will find that they are outputed after all inputs have finished, so output has a buffer.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
musa
  • 11
  • 3