> How to stop C++ from immediately closing program?

How to stop C++ from immediately closing program?

Posted at: 2014-12-18 
Try adding the line '#include ' at the top, just under the line '#include '. Then put these two lines at the very bottom just before the closing '}' symbol.

'system("PAUSE");'

'return 0;'

This presumes that you are using old style header statements and that you do not include the single quotes I did when you type it in. It should work in Windows with the added message "Press any key to continue..." displayed until you take action at the keyboard. You see, there isn't anything wrong with the code other than it isn't being specifically written to call and hold a window, so Windows, in all it's manifest intelligence and glory, needs you to tell it something to do until some user takes a final action or the window opens and closes without giving you time to read anything as the program completes. The inclusion of stdio.h allows you to use the system() function it provides to pass a system command to the operating system itself. In this case, using the command "PAUSE" (no particular reason to capitalize it save code readability;-) makes the os print out 'Press any key to continue...' and wait for a keyboard hit to release the program execution. The return statement is required nowadays to be appropriate for C++ main functions, whether the compiler you're using is up to date or using relaxed standards switches or not, so I had you include that for simple compatibility reasons and program correctness.

I hope that this will help you with your console programs under Windows from now on. Good luck!

Hey, guys. I'm taking a Computer Science class and I recently started learning how to use C++. I have a problem with it. Every time I execute the program, it immediately closes after it's finished displaying the result. Here's my code. I hope you guys can tell me what's wrong with it.

#include

main()

{

int P1;

int P2;

float m;

printf("digite a nota da P1: ");

scanf("%d", &P1);

printf("digite a nota da P2:");

scanf("%d", &P2);

m=(P1+P2)/2;

printf("media aritmetica=%1.1f", m);

}