Sign in to follow this  
egowaffle25

Command Promp Closing

Recommended Posts

I am quite a beginner to game programming. I decided to start out with C++, so I create my 1st program, A Hello World one. It compiled fine, however when I tryed to open it, the command prompt window opened and closed instantly. Anyone have a solution to this? I am using Microsoft Visual Studio .NET 2003 on Windows XP Profesional Edition.

Share this post


Link to post
Share on other sites
Quote:
Original post by directrix
You can use the function getchar() and put it as the last line in main. getchar() causes your program to wait for a key to be pressed before continuing.

*** Source Snippet Removed ***


I'm assuming the getchar() goes in before "return 0;"

Also, is there any way to fix this system-side and not code-side?

Share this post


Link to post
Share on other sites
I'm pretty sure that you can't fix this system side as there's nothing actually wrong. Mostly people either add a getch() or a cin.get() at the end, while others just actually open up the command prompt and run their code there. As for where to put the getch, it is before the return 0, yes. Good luck with C++.

Share this post


Link to post
Share on other sites
Yeah, that's normal behavior. Most command-line programs are expected to exit immediately, rather than wait around for the user. This is because (for the most part) command-line tools are usually supposed to be simple input-output driven utilities that can be chained together or run in shell scripts, and this idea would be kind of broken if all the programs waited for a keypress from the user before exiting.

In the case of "Hello, World" programs or command-line applications that are intended to be user-interface driven, you just need to write your code to produce this behavior - i.e., wait for input before continuting with certain actions, or before returning from main().

Share this post


Link to post
Share on other sites
Quote:
Original post by egowaffle25
Also, is there any way to fix this system-side and not code-side?


As others have pointed out, the fact that the console window gets destroyed at program exit is normal behavior. Since console programs are intended to be run from within a console, the best solution really is to create one such (run cmd.exe) and to invoke your program from it. Barring that, you can create a batch file that calls your program and then PAUSE. It's basically equivalent to sticking a system("PAUSE"); in your code, except cleaner. [smile]

It used to be possible to specify (for DOS/Win16 programs) that the console window shouldn't be closed on exit, but it doesn't seem to be an option with Win32 console programs.

Share this post


Link to post
Share on other sites
Most ide's will give the option to automatically put a "system('pause');" of sorts on your program, some by default. User's unaware might load up their program in another ide (or run it on it's own) and still expect the pause to be there.

Share this post


Link to post
Share on other sites
If you are using Microsoft Visual Studio .NET 2003, all you have to do is pressing Ctrl+F5 when you want to run your program. You can run it by pressing just F5 but if you do so, your program will close because it is in Debug mode. If you run it with Ctrl+F5 the program will behave as if it has a system("pause");

However, if you want to run your program outside Microsoft Visual Studio, then you will have to add system("pause"), cin >> ..., or something like it... =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this