Win32 console disapears after a second - c++

I have created a Win32 Console Application in Visual Studio but when I start the program the console apears just for a second and then disapears again. What should I do that the console remains on the screen ?

Well, the program has finished running, so it closes.
Either make the program wait for input (e.g. with getchar()), or press Ctrl-F5 to run the program without debugging (but then you won't be able to set breakpoints and stuff).

You can set breakpoints anywhere in your code to make it stop. If you just want to see the output of the program when it's done, try setting a breakpoint on the last line of main().

This is happening because the program has nothing to wait for before exiting.
Try running std::cin.get(); before main() returns to make the console wait for keyboard input.

Related

Force C++ program to pause in Visual Studio debugger

I'm debugging C++ program compiled with MSVC under Windows.
I want to investigate issue linked with multi threading. So I put ASSERT in my code and when program reaches ASSERT it displays window about ASSERT (Standart [Abort], [Retry], [Ignore] window) with proposal to pause program in debugger. I press [Retry] button and program pauses. BUT while I was pressing the button other threads continue to execute.
So the question is how to immediately stop the program when it reaches some point to see what other threads was doing at that time?
You might want to set a conditional breakpoint instead of using an assert:
In case you want to do it programmatically, use DebugBreak. (C# has an equivalent api System.Diagnostics.Debugger.Break)
In case you want to do it from ide, from the msdn page you can put a breakpoint (or break all the application, ctrl+alt+B) from visual studio and then control the thread execution using "freeze" and "thaw" in the thread window.

click close console window to end a c++ console program is proper way?

I have just got this problem for a few days. Before, I've always thought that letting the program exit by returning from main and clicking close the console window is the same way to end the program.
However, I've found that they are different. Since my program opens a camera which is an object. And closing the console windows does not destroy or clean up the object. So the next time I have error to open the camera again
I just need a confirm if this is true?
Then why only until now I can see the problem?
Closing a console window in Windows, kills the running program (or stack of running programs). Unless it has registered a handler for this event, it gets no chance to clean up. If you want solution, register a handler.
Hm, consulting the documentation, wait a few secs…
OK, look up SetConsoleCtrlHandler.
Closing a running console application will kill the process, not giving you the chance for any clean up code. I guess you could hook a windows message loop to trap the WM_CLOSE message and do proper cleanup, but at the end of the day, you just shouldn't kill the process.

GUI app with redirected stdout stream doesn't return control back to the console

I have a GUI Windows application written in C++ (with MFC) that attaches itself to a parent console for an output via stdout stream. It uses the AttachConsole API and the RedirectIOToConsole method described here.
So if I call the following line from a regular console application (that natively has the output to stdout):
_tprintf(_T("Test message.\n"));
I get this output that I'd expect:
But if I call the same line of code from my GUI app (with an attached parent console) I get the following, except that the control doesn't seem to be returned back to the parent command prompt window (see red circle):
Note that I can type into the parent command prompt window, so it's not hanging, and if I hit enter it will execute whatever I typed as a command.
So what have I not called from my console app to return control back to the parent console?
It's not about returning control.
When you run a console application from a command prompt the command prompt waits until the console application finishes, so you see the output from the console application followed by a fresh prompt, as your first picture shows.
When you run a GUI application from a command prompt the command prompt doesn't wait for the GUI application to finish, so you normally see a fresh prompt followed by the output from the GUI application, like this:
C:\>TestGuiProjWithOutputToConsole
C:\>Test Message.
_
As you've noted, nothing has gone wrong. You can type commands and they work, it's just that the prompt has appeared in the wrong place.
Your second example doesn't show the second prompt, maybe because you've got a carriage-return character so the prompt is being overwritten. Try outputting a newline before the test message and you should see the prompt (though still in the wrong place).
You can tell the console to wait for a GUI application by running:
start /wait TestGuiProjWithOutputToConsole
In short, GUI applications don't play well with the command-prompt because they are both running at the same time outputting to the same console so their output is jumbled up.

C++ on Windows - the console window just flashes and disappears. What's going on? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Visual Studio Console App - Prevent window from closing.
I'm starting to learn C++ on Windows and I'm trying a few different development environments:
1. Netbeans with Cygwin compiler
2. MS Visual Studio 2010
For either of them, when I write a very simple Hello World program, I build it and it's fine. But when I try to run the program, the command prompt window pops up really quick and then disappears right away.
This happens whether it's in Debug or Release config. Please help with this - I can't see my program output! :(
Thanks.
EDIT1:
Thanks for the replies. This is my code:
#include <iostream>
int main()
{
std::cout << "This is a test." << std::endl;
return 0;
}
I tried Ctrl+F5 for "Start without Debugging" and that doesn't work. It still flashes the black console screen then disappears right away.
I also tried adding in std::cin.get(); and this works with Ctrl+F5, but isn't that a really... inelegant workaround solution? I'd rather have my program in the final form.
The breakpoint works, but then I have to run with debugging and the console window flashes and disappears, but then it stays in the background. Any way to get the console to stay in the foreground so I can see program output right away? Seems like that's how it should work.
Any more ideas? Why wouldn't Ctrl+F5 work?
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Write cin.get() at the end of the program.
use Ctrl+F5 to run your program or set a break point in the last line or write cin>> to any vraiable at the end....etc
I think your program just prints Hello World and then exits. That's the reason the console closes immediately. You can run the executable from Command Prompt (Start Menu > Run and type cmd.exe). Otherwise, you can put std::cin.get() in your code so that program waits for user's input and hence the console window remains open until a key is pressed.
Your application is probably working. Make the last command in your console application wait for user input: e.g int i;
string i;
cout<<"Hello";
cin<<i;
Issue a getchar() before returning or run from cmd.exe

CPP:How to pause resume a running script in cmd line console using mouse?

I am in desperate need of help.... :). I am running a script from cmd line console. It does a sequence of operations. whats the best way to pause the script in between to check the results and resume it back using mouse????? or any key
I would appreciate for your reply back,
-Abishek
If I understand your question correctly, and if your program is writing to the standard output (the console window) and you are running on Windows:
You can turn on 'quick edit' in the console window by editing the cmd.exe window properties.
Open a command prompt (start | run | cmd.exe)
Click the upper left corner of the window and choose 'Defaults'.
Click the Options tab and then in Edit Options section make sure the QuickEdit Mode box is checked.
Click OK and you should be set.
If you click with the mouse inside the console window when quick edit is enabled, it will block the program from continuing while it is waiting to be able to write the next message to the output. If you then hit 'esc', the program will continue.
It's a useful shortcut for pausing a running console program that I use all of the time.
try cin.get()
There isn't any easy way to do that. You'd need to create a wrapper to run the program (for example, a debugger is a wrapper to a program), and this wrapper would have to have mouse functions programmed into it.
Without use of the mouse, use cin.get();. It will take an input from the keyboard before continuing.
I'm not sure about using the mouse to pause, but you can pause the execution of command line process by pressing the Pause / Break key or Ctrl + NumLock .
You can then resume the execution with Ctrl + Z