Tuesday, July 14, 2009

I am coding on Visual Studio 2008 in C. How can I stop the program terminating automatically?

Basically the code runs and what is there works well but I am just starting on how to code (read as: copying examples and following them) and would like to know how I can stop it automatically closing before I can see its outcome.


Also if you can explain the code required to stop this occurring and how it work much appreciated.





Cheers

I am coding on Visual Studio 2008 in C. How can I stop the program terminating automatically?
The key is to do a console input of some type (getchar(), scanf(), ...), causing the program to halt with the text in the console window until you press Enter. A message to the screen just before this is also useful to know you've reached that point and aren't just hung somewhere earlier.





Hope that helps.
Reply:Two ways:


1. Run the program manually through the command prompt. i.e. Start| run| enter "cmd"





2. put a sleep (x seconds) in your program.
Reply:You could use getch() at the end of your program, which will wait for you to press a key before it terminates. Alternatively you could run the application from the command line.
Reply:Try adding system("PAUSE"); at the end. If you return 0; do it just before that.





System() calls an external program and Windows has a program called PAUSE. You should be all set.


No comments:

Post a Comment