Share via

How do you make the Command Prompt window stay open after running a command?

Anonymous
2009-02-22T07:34:36+00:00

Ok, I feel foolish asking this, because I knew the answer before. I'm learning how to program Visual C++. The book I have teaches how to write C++ and then goes into detail on writing the GUI part of Visual C++. The book I have walks me through compiling and making an exe file out of a cpp file, and then it says to go to the directory in Explorer and double-click on the exe file. I do this and the Command Prompt comes up for a quick second before closing down. The example in the book shows the output, and obviously the writer has his Command Prompt  configured to pause after executing an exe.

I just want to configure my Command Prompt to do the same thing, so I can at least see the results. I know that the exe I wrote is working, but I'd like to see the output for less than one second.

Windows for home | Previous Windows versions | Accessibility

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2009-03-04T18:40:43+00:00

I'm not sure how you are coding this. but if its like a normal batch or anything command line related. 

Do the following to use a user defined switch at the end of the script.

To pause a DOS batch file in Windows XP from continuing to the next command, you can use the PAUSE command. Doing so prompts the user with the text:

Press any key to continue . . .

Then the batch file waits for the user to press a key. However, what if you want to pause a batch file and not display anything? Just pipe the results to nul like so:

pause > nul

Since this may not be very useful (how will the user know they need to press a key to continue the batch file?) you should display a different prompt. Issue an ECHO command with your desired prompt, then issue the above pause > nul command.

For example, to pause a Windows XP batch file with the prompt "Press ENTER to execute the command", use the following commands:

echo Press ENTER to execute the command pause > nul


MCP. MCDST. LVL 80

Was this answer helpful?

20+ people found this answer helpful.
0 comments No comments

25 additional answers

Sort by: Most helpful
  1. Anonymous
    2009-02-25T22:40:57+00:00

    Thrydmon, try the following

    press the windows key + r on your keyboard to open up run

    type cmd.exe /kpress enter.

    this switch will keep the window open.

    does this serve what you are asking for?


    Rami

    Microsoft Answers Support Engineer

    Visit our Microsoft Answers Feedback Forum and let us know what you think.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2009-02-22T13:36:33+00:00

    Hi Thrydmon,

    Thank you for posting in the Microsoft Answers Forums.

    You most likely compiled and run it inside an IDE. The console behavior will depend on how you configure the IDE.

    To solve this, either:

    1. Open command prompt, "Start->Run", type "cmd", hit enter. Then once the command prompt is open, browse through the directory where you compiled the program and type the program name. This will not close the command prompt unless you have something else in your code that will force the console to close.
    2. Another way is to put:

    system ("PAUSE");

    right before your return 0; (this being done in cmd)

    This will make the program with until you press a key to continue.

    Please post back and let us know if either of these helped to resolve your issue.


    Robyn

    Microsoft Answers Support Engineer

    Visit our Microsoft Answers Feedback Forum and let us know what you think.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  3. Anonymous
    2009-02-25T22:47:24+00:00

    also use cmd.exe /? for a listing of more switches


    Rami

    Microsoft Answers Support Engineer

    Visit our Microsoft Answers Feedback Forum and let us know what you think.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2009-02-22T23:54:06+00:00

    Thanks, Robyn,

    While that isn't the exact answer I was looking for, I did know how to run the executable from an open command prompt. Also, I wasn't looking for code to put into my C++ file to pause the screen (mainly because while that works with Windows, it may not be usable in all OS's).

    I thought there was a way to configure the cmd so that it doesn't immediately close after executing an exe file through double-clicking it in Windows Explorer. Of course, all this program does is print out "Hello World!". (I really wish that these programming books would give something more difficult than "Your first program will print "Hello World!"".)

    Was this answer helpful?

    0 comments No comments