Command Promp Closing
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.
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.
int main(){ // your code... getchar();}
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?
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++.
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().
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().
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.
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.
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... =)
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... =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement