views:

114

answers:

5

When I write a program using C++ and I want to run it, I can't catch the console window. I press CTRLF5 and it does not work.

I want the window to stay open and wait, even it finishes executing. Can anyone help me?

Thanks in advance.

+1  A: 

try using system("Pause"); as the last line on your code (before the return of your main function)

Ars
i tried it ... but i don't want to "pause" it ... i wanna the program to finish executing ...because when i want see destructor for some class it won't work until it finish at all ... got me??
Ruba
Encapsulate your program in a method, call that method from main(), and add cin.get() after the method returns. This is a windows 'feature'
Falmarri
i'll try it.. thanks:)But i didn't understand why there is this issue with vs2010?? it wasn't in vs2008
Ruba
Ctrl+F5 works for me (I'm using VS2010), it says "Press any key to continue ...", can you show us your source code ?
Ars
@Ruba: It looks like you want to put a breakpoint at the destructor of a global object. Is that correct? Instead of Ctrl+F5, do simple F5
Chubsdad
I am not sure if it will change anything but try system("pause"); without the capital P.
Alerty
Case shouldn't matter but, based on comments, I don't think that's what OP wants. There may be something being output by the destructors of objects created in main (or static objects). Pausing before exiting main will not show that output.
paxdiablo
@Ars : i'm studying c++ in my college ... so i'm trying alot of codes ..not specific program ...
Ruba
Can you try to debug step by step at the end of your program, and check if there's any exception thrown.
Ars
+1  A: 

Ctrl+F5 should work. Just in case, if you have the source of your program, add the following just before the closing brace of main.

int x;
cin >> x;

the program will wait for you to enter some value.

If you want a breakpoint to be triggerred in debugger, do simple F5 instead of Ctrl+F5, after putting a breakpoint on the relevant source line (assuming the source/debug symbols are available)

Chubsdad
A: 

Sorry to say, Ruba, but it looks like Microsoft removed this nifty little feature when moving from VS2008 to VS2010.

I can't find anything on MSDN, the web in general, or VS options to turn it back on.

My advice is to bypass the environment altogether for testing your application. Simply open a cmd.exe window in your runtime directory (debug or release or whatever), build the executable within the IDE then switch to the command window and enter testprog.exe to run your program.

Make sure you include any required command line parameters and, after you've entered it the first time, you can just use the up-arrow to retrieve the last command.

Yes, it's a bit of a pain but, until someone comes up with a better solution, it's probably the best way to ensure you see all the output while ensuring the program has shut down completely.

paxdiablo
Why would Microsoft do something like? :S
Alerty
I decline to comment on that one, @Alerty :-)
paxdiablo
A: 

Just set a breakpoint at main()'s closing curly brace if you want to see the console after the program is finished.

John Dibling
adding breakpoint didn't work with me .the best solution for now i think is to put my whole code in some function and call it in the main .. the i put cin>>i for example in the main at the end
Ruba
A: 

Make sure that your project is explicitly configured to use the console subsytem.

Open the project properties (right click the project and select Properties), then go to Configuration Properties -> Linker -> System.

Make sure that SubSystem is set to Console (/SUBSYSTEM:CONSOLE). The default subsystem is Console, but you still have to set it explicitly to get Visual Studio to recognize it. This was also the case in Visual Studio 2008, so the behavior hasn't changed.

James McNellis
@James: i checked it and it's the same you said it Console(/SUBSYSTEM:CONSOLE) ... :(
Ruba