Help Me Please!!!

Started by
1 comment, last by mikeman 17 years, 7 months ago
Hi I am a complete newbie to programming so im starting with c and then im going to work my way up, i have a compiler (Dev-c++) and a few tutorials im learning but im having a problem im running windows xp and when i write the program and compile and run it the window closes down before i can even see anything what am i doing wrong? its driving me mad there is a help area with the program that says what u should do but this just brings up errors if any1 can help it would be much apricatied,!
Advertisement
That is the normal behaviour for a console application. They are designed to be run from a command prompt, in which case their output stays on the screen, but when you run them from within Windows, a new console is created to run your application which exits when the application has terminated.

Before you are innundated with suggestions to put system("pause"); as the last line of your program (which will technically work) let me suggest some better solutions:

1) If your IDE supports it, set a breakpoint at the end of your program. This removes the artificial "wait for a keypress" from your program, which you only want there while developing, and moves it into the development environment.

2) Run your program from a command prompt rather than from the IDE. Please feel free to post if you need more information on this.

3) Create a batch file containing:

c:\whatever\my_program.exe
pause

and run your program by double-clicking this file. This again removes the pause from your program itself.

4) If you must put the pause in your program, rather than use the non-standard system("pause"); consider using:

std::cin.get(); // this is standard C++ but will be "fooled" on occasion by other input still in the stream

or see if your compiler has an include file <conio.h> and a function getch(); which will actually physically wait for a keypress.

system("pause"); at the end is an okay-ish solution if you are just beginning but you need to be aware that it is not a good long-term solution. There is a lot to be said for using a command prompt not just for running console apps but for developing them as well, although that does deny you the advantages of an IDE and debugger. Also, if some unscrupulous person was to put a program called "pause.exe" in the same directory as your program, that would be run instead of the system pause command, with all the security implications that brings.
It's supposed to behave this way. Just run your program through the command line. That way the console window will not close when your program terminates.

This topic is closed to new replies.

Advertisement