Monday, June 30, 2008

Console window appears & immediately disappers on running code

When you run a code, the console window appears and immediately disappears. So you don't get a chance to see the result or enter and user inputs. This happens with some Windows based compilers.
The solution to this problem is to include either of the following statements at the end of your C/C++ code.

getch(); (Press any key to exit)
or
getchar(); (Press Enter to exit)
or
getc(stdin); (Press Enter to exit)
or
fgetc(stdin); (Press Enter to exit)
or
#include <stdlib.h>
system("pause");
(Not recommended)

Example

#include <stdio.h>
{
printf("Hello, World");
getchar();
}


Online C/C++ compiler: http://www.techbugs.org/wiki/OnlineCompiler

No comments:

Post a Comment