views:

924

answers:

8

Hi, I'm trying out Visual studio 2010 by compiling a C program. After it displays the solution in the "DOS" command window, the window is immediately closed. In Visual studio 2008, one gets the message press any key to continue and pressing a key closes the command prompt window. How do I set this behavior in 2010?

Thanks a lot...

+2  A: 

This is normal. The "DOS" console window is attached to your program and is supposed to exit when your program finishes. If you want it to stay open, you need to add code to your program to keep it open. All you have to do is add a print statement and then input statement to the end of your program.

Joel Coehoorn
Ok thanks...a code to accept an input from the keyboard.
yCalleecharan
A: 

If you start your application without debugger ("Start without debugging"), the console window will remain open until you press a key.

Cătălin Pitiș
hmmm...actually I did that. I know this also applies in Visual studio 2008 and this "command" is what I use to run my programs. So I was a bit surprised of this different behavior in 2010.
yCalleecharan
I can confirm that using "start without debugging" in 2010 doesn't keep the console window open after the program exits.
yCalleecharan
Now, that is strange. I don't have the same behavior on my Visual Studio...
Cătălin Pitiș
Yes, with Visual studio 2008 it behaves as expected. I've restarted my machine but I still get the same strange behavior with 2010.
yCalleecharan
+1  A: 

You can add this function and call it right before returning from main():

void keep_window_open()
{
    // clear buffer
    fflush(stdin);
    printf("Please enter a character to exit\n");
    char ch;
    ch = getchar();
    return;
}

I adapted it from a C++ version in a utility header file at Bjarne Stroustrup's site: http://www.stroustrup.com/Programming/std_lib_facilities.h

Darel
Thanks a lot. I had to move the line "char ch" just after the opening brace { after the function definition so as to avoid compilation errors. I think that the line "fflush(stdin)" is C++ code and doesn't work well in C.
yCalleecharan
You're right, according to the c FAQ:http://c-faq.com/stdio/stdinflush.htmlfflush is only defined for output streams.The link off that FAQ entry discusses how to flush input:http://c-faq.com/stdio/stdinflush2.html
Darel
Thanks for the link.
yCalleecharan
1 vote up. This piece of code has been a savior. But vitavim (see the post on top) has found a correct solution for this bug and I'm accepting vitavim answer.
yCalleecharan
A: 

FWIW, the "Start Without Debugging" command works in VS2010 exactly the same way it does for me in previous Visual Studio versions (that is, for a console project the console remains open when the process terminates with a "Press any key to continue . . ." prompt).

So, exactly how are you getting VS2010 to run your program? If you're using the "Ctrl-F5" shortcut, can you verify that Ctrl-F5 is bound to Debug.StartWithoutDebugging in "Options/Environment/Keyboard"?

Also, can you try running your program using the menu ("Debug/Start Without Debugging")?

Michael Burr
Thanks. First I compile the file, then build it and then use Debug > start without debugging. I have assigned the "Ctrl-F5" as you told me but I get the same behavior. I admit that it's strange as Visual studio 2005 works fine as expected.
yCalleecharan
@yCalleecharan: Interesting - I'm unaware of a setting that controls this.
Michael Burr
Yes, it's hard to believe this strange behavior.
yCalleecharan
A: 

Has anyone found a solution to this besides adding additional code at the end?

Daryl
It doesn't seem so.
yCalleecharan
+4  A: 

After a bit of googling, I found this solution that does not involve modifying your code. It contains a workaround that involves modifying your .vcxproj file.

To do this from within Microsoft Visual C++ 2010 Express (I'm assuming it is similar for Visual Studio 2010), open your project and navigate to the following menu:

Project->$YOURPROJECTNAME Properties...
    ->Configuration Properties
        ->Linker
            ->System->SubSystem

Then use the dropdown to select Console (/SUBSYSTEM:CONSOLE) and apply the change.

"Start without debugging" should do the right thing now.

vitavim
Wonderful. This works very fine. And I gave you 1 vote up also. I'm using the Express edition.
yCalleecharan
Many thanks for reporting this bug here.
yCalleecharan
A: 

What jackass at Microsoft thought this was a change for the better?

Richard Nixon
A: 

tools->setting->expert settings

sianko