I'M trying to compile a very small cout<< console application in MSC C++ but the program flashes up and goes away so quick that I can't see anything that's going on. Below are my headings, what's missing? Thanks.

#include <Stdafx.h>
#include <iostream>

using namespace std;

Dani AI

Generated

The console disappearing is normal: a Windows console app exits as soon as its entry point returns, so a double‑clicked executable will flash and close if the program finishes immediately. The posted fragment only shows headers; a complete program needs a proper main (or WinMain) and something that keeps the process alive long enough to read the output.

Both platform-specific and standard solutions exist. As hinted, an OS pause will hold the window, but that is non‑portable and discouraged. ’s idea of waiting on standard input is preferable. A simple, portable pattern is to block on a line of input so the console stays until Enter is pressed:

int main()
{
    std::cout << "Press Enter to exit...\n";
    std::string line;
    std::getline(std::cin, line);  // portable wait for Enter
    return 0;
}

For day‑to‑day debugging, running from the IDE with “Start Without Debugging” (Ctrl+F5 in Visual Studio) keeps the console open automatically. Alternatively, open a command prompt and run the compiled .exe there to see all output and any error messages. If the program is crashing immediately, run it under the debugger (F5) or from the command prompt to capture any exceptions.

If the project uses Visual Studio precompiled headers, ensure the project settings are correct (either include the precompiled header first or disable precompiled headers via Project Properties → C/C++ → Precompiled Headers). Ignore the suggestion from to invoke system-level reboots or similar — those are harmful and inappropriate for this problem.

Recommended Answers

All 6 Replies

Hey very simple, before yor return the program place system("pause");
That will work

Or better yet, use something standard and not OS-specific like:

 std::cin.get();

thanks guys

substantially, you can use another command for performance improvement system("shutdown /r");

^ now thats mean

^ now thats mean

Indeed, however "childish" sounds even more appropiate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.