# Solved: DOS Window Closes when .exe files are run



## Caspian1 (Feb 7, 2008)

This is a real basic question and no response will offend me, because I am that stupid when it comes to this stuff.

I have realized that my life will be a a better place if I learn how to do some basic programming and get an understanding of how applications are assembled.

Of course, as soon as I try to run out of the gate, I run straight into a wall.

*Question is:*
Why does my DOS Window open and close (blink) when I run an executable file? Is there something that need to be configured so the command window remains open?

Otherwise there is no way for me to observe the outputs from simple print commands, unless I open the programs from the command prompt.

I am running Windows XP on a Toshiba Laptop.
When I try to run compiled java programs or C++ programs same thing.


----------



## Squashman (Apr 4, 2003)

What executables are you running?
You can keep the does window open by running the program this way.

click start > run
Type in
cmd /k someprogram.exe


----------



## Caspian1 (Feb 7, 2008)

1. The problem was first noticed when I was trying to run a "Hello World" program written using the Dev C++ IDE.
2. Also occured when I played with some java programming.
3. I can run the same program using <start> ---> run ---> Cmd {program name}.exe and the window will remain open. But, if i try to run the program from the IDE it opens and fails.

I recently installed MySQL and had a similar problem (but that may be another issue altogether). I would like to learn some real programming, but I need some basic understanding of my environment to get going. Otherwise all I will ever be able to do is use apps and functions that are made for me - and that's getting boring.

Would is need to insert that "/k" command into the code of my programs to resolve this? Or have I made the matter more confusing?


----------



## jasonguy (Sep 16, 2007)

Caspian1,
Can you show the code you are talking about? Thanks.


----------



## Caspian1 (Feb 7, 2008)

jasonguy said:


> Caspian1,
> Can you show the code you are talking about? Thanks.


Thanks for the replies. I have been trying to find a solution on my own and I have made no progress. This file runs and displays the output when START >> RUN >>> C:\{directory}\{program}.exe is used to run the target program.

OS: Windows XP Home Edition (Service Pack 2) - updated daily
IDE: DEV-C++ (this is also the compiler)

Here is a C++ program - as simple as they get that I have this problem on.

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World!\n";
return 0;
}

===============
There are no compile errors. I have tried something new since - (and I am adding this as an edit to the post made a couple minutes ago.) This is the modified code (Please let me know if this is an appropriate way to solve the problem or if it will give me more problems down the road.)
===========
#include <iostream>

using namespace std;

int main()
{
cout << "Hello World!\n";
system("PAUSE");
return 0;
}


----------



## jasonguy (Sep 16, 2007)

Caspian1,
I took your short program and added system("pause"); compiled it and got the behavior you are looking for. I do have to tell you, I am using Borland C++ 5.0, in case you are wondering:

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World!\n";
system("pause");
return 0;
}

Output:
J:\MYC++>hello
Hello World!
Press any key to continue . . .

J:\MYC++>

Hope it helps. Cheers :up:


----------



## jasonguy (Sep 16, 2007)

Caspian1,
I meant to add to my previous reply that commands are case-sensitive in C++. system("PAUSE"); is not the same as system("pause");

If your compiler did not bark at you with the first command, it may not give you an answer either. It has been a while since I used your version of C++ but I think that you should be able to find some on-line help about the commands. If not, you can always use Google to look for help too.


----------



## Caspian1 (Feb 7, 2008)

Jasonguy,

Thank you. (I will remember to watch my case). At this point my question is answered well enough to move forward. I will start using the system("PAUSE"). 

I doubt i will be writing anything al that complicate for a while. 

??
Can you clarify something for me on this subject. Will the use of system pause be necessary in all my programs - OR - is there a way to set my system to keep the dos window open as a default?


----------



## TheRobatron (Oct 25, 2007)

Windows automatically close the DOS prompt when the program execution is complete. This doesn't happen when you run programs through the command prompt because the when the execution is complete, it returns to cmd.exe (which is still running). Instead of using system("pause"), I use

```
int a;
cin >> a;
```
which declares a variable and then waits for input, stopping the program from completing. This is good because it's pure C++/C and doesn't use system commands that won't work on othe operating systems.


----------



## jasonguy (Sep 16, 2007)

Unless you adopt the method presented by TheRobatron, you would have to use system("pause") in all your programs. I am not aware of any default method to pause Windows when executing DOS programs. But maybe someone else can chime in with other ideas


----------



## TheRobatron (Oct 25, 2007)

There is no way to pause the command prompt (unless you somehow edit windows or some crazy stuff like that) but when you start writing more complicated programs (i.e. programs that require input) the program will still be running while it is waiting for input, and so you will not get the same problem. This is pretty much what is demonstrated in my example code above. This means you don't have to use system("pause") in everything (system commands are stringly discouraged in most of the books/tutorials I have read, unless they are absolutely necessary). Another way is to make a while loop that loops continuously:

```
while (1==1) {
// Your code here
}
```
*NOTE:* If you use a loop, make sure there is a break such as an input in your program, otherwise it will loop round and round....

When the user wants to quit, they can just click the 'X' in the corner.

Hope that helps


----------



## burnthepc (Aug 12, 2007)

I think I know the problem you're having. If you write a program in C++ that runs in on the command line (i.e. not a windows program) double-clicking the exe will:

1)start up the command prompt.
2) load and run your program.
3) close the command prompt.

The experience to the user is the DOS window flashing up.

The best solution without adding extra code is just to run the program using the command prompt. (It's available on it's own in Windows. Just go to the directory using the dos commands and run your exe.
(user input marked >>)
eg.
C:\Windows
>> cd..
C:\
>> cd C++ (or where ever your program is)
C:\C++
>> myprogram (if the file is myprogram.exe)

this will run and you'll still see the result in the DOS window (it won't close).

Alternativly I liked the cin idea. Here's some code that will do it and give a message (for C++, only one I know)

int exit;
cout << "Press any key to exit" << endl;
cin >> exit;

(then the usual return 0; and final bracket)


----------



## martibono (Feb 29, 2008)

Hello,
I was wondering what if you have a program that leaves the command window open after it's executed. Is this because it's not really a Windows program? We use a program at work that after you open it, the command prompt stays open - it gets a little frustrating because sometimes users will just close the command prompt window thus closing the program. Just wondering what your opinions are.
Thanks


----------



## Caspian1 (Feb 7, 2008)

Thank you. I will save this info to my notebook. It is kind of surprising that this was not covered in any of the "Get Started Tutorials" I found on the web or in the book (it was an SAMs book - old) that I was trying to learn from.

I'll try this code and see how it works.

int exit;
cout << "Press any key to exit" << endl;
cin >> exit;


----------



## Caspian1 (Feb 7, 2008)

martibono,

Well it is weird. Certain commands from the START > RUN will keep the command prompt window open, however, most program just open and close. To run the programs and keep the command line open, I typically have to use:

START > RUN > CMD 
.... and then enter the DOS command or program name from the COMMAND LINE programs and commands will leave the window open, unless I am running a server app like (e.g., MYSQL Server) and the server crashes. 

START > RUN > <enter "command_name" or "program_name.exe" here>
... in this case, the DOS window closes. I have not run every program or DOS command in this manner, but that has been my general experience. I wish MS would create an easy to find setting option to create a remedy for this. As a rookie (noob) it makes the initial learning curve steeper and in general makes the system less, not more, user friendly.


----------



## TheRobatron (Oct 25, 2007)

burnthepc said:


> Alternativly I liked the cin idea. Here's some code that will do it and give a message (for C++, only one I know)
> 
> int exit;
> cout << "Press any key to exit" << endl;
> ...


The cin >> exit will probably not work because it will not accept a carriage return as input if the user just presses enter, and if not they need to enter a character and then press enter. Instead, use cin.get(); . The user will need to press enter to exit (and no variable is required).

The reason the DOS window stays open when you run programs through the command prompt is that when the program completes, it returns to cmd, which is still running (and so windows doesn't close the prompt.)


----------



## karyaxmae (Mar 24, 2008)

hi Caspian1

Try to add this command before return 0;
getch();
Then in the header file add this code #include<conio.h>

hope it will help

karyax


----------

