2

When I try to debug my little program it closes immediately. This is the code I am using.

#include <iostream>

using namespace std;

int main()
{
    int gt1, gt2;
    cout << "Hello World!" << endl;
    return 0;
}

Just for 0.5 sec a black box shows up and it closes.

Is there anything I can do to prevent this?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
Alex de Lange
  • 23
  • 1
  • 3
  • add this line before `return 0;`: `system("pause");` – Krishnachandra Sharma Feb 21 '13 at 09:17
  • If you want to print a goodbye message or something, the user can see it. – Alex de Lange Feb 21 '13 at 09:21
  • 1
    @AlexdeLange The user will not be running your program from a debugger, but (most likely) from a console window. In such case, the output will simply remain in the console window. – Angew is no longer proud of SO Feb 21 '13 at 09:25
  • @AlexdeLange: console programs normally will be run from console. So all those `system("pause")`, `cin.get()` and `getch()` suggestions will make the usual (i.e. console) appearance a bit awkward, because the user has to hit some key for no obvious reason. Don't change the code because your debugging habits don't fit the debugger's behavior - change your debugging habits (i.e. set a breakpoint if you don't want the program to terminate so fast). – Arne Mertz Feb 21 '13 at 09:40
  • Related: [Is there a way to catch the stderr and stdout in Visual Studio?](http://stackoverflow.com/q/10238331/1084416) – Peter Wood Feb 21 '13 at 09:54
  • Why is this question closed? It is not about preventing program from exiting immediately, but about stopping its execution in the IDE (but question is vaguely formulated). Even if it is a duplicate, the cited link is invalid. – Spook Feb 21 '13 at 12:31

3 Answers3

2

Set a breakpoint in your code, such that you can debug it. Just click on the bar on the left side of the source.

eBreakpoint

Spook
  • 25,318
  • 18
  • 90
  • 167
0

Your function completes and returns the control to the debugger. That's why you see the screen close. This behavior (debugging) is different than running your program from Visual (where you see the line "press any key..." in the end.

For debugging purposes you can add the following two lines just before "return":

char ch;
cin.get(&ch);

This will wait for any key being pressed before termination.

Vladik Branevich
  • 1,180
  • 8
  • 11
  • 1
    If something remained in the stream, it will terminate the program anyways. And if not, the program will just stop, telling the user nothing and he will wonder why nothing is happening. Don't introduce bad usability just to make debugging more "comfortable". – Arne Mertz Feb 21 '13 at 09:42
  • I merely explained what was happening in this very example and, BTW, in this specific case, there's _nothing_ in the input stream. This was not about teaching right and wrong, so don't get too mentor-like, please. – Vladik Branevich Feb 21 '13 at 11:49
-1

Add cin.get(); to end of code before return 0.

Victor Buldakov
  • 105
  • 3
  • 11