Learning C++, DOS prompt insta-closes, why?

My first time learning C++ and I’m trying to write a couple basic programs, I’m using an open source compiler (Dev-C++) and I wrote a couple programs but all of them have the same problem, I tried using a different library but it doesn’t seem to matter.

For example;


#include <iostream>

int main ()

{
    std::cout << "Hello World
";
    
    return 0;
    
}

I entered this and then compiled with no errors, tried to excecute and then I get a brief flash of MS-DOS Prompt opening and closing, I can barely see it enough to tell if the message came out right. :confused:

All the resources I’ve been reading don’t mention anything about this, and I don’t understand whats wrong, I tried using <stdin.h> with printf("Helloworld
"; but that didn’t work either.

Am I doing something wrong? Is my compiler broken? Do I need to include some sort of timer or something?

I assume you’re launching the executable from windows by double-clicking an icon? If so, the easiest way around this is to launch it from a DOS prompt by typing the filneame, instead. Later versions of windows close DOS windows as soon as the program finishes executing when launched from within Windows. You can also loop an input command, so the window won’t close until you hit a key, or even a specific key.

[ul][]Open the directory where your executable resides with Windows Explore.[]Right-click on the icon for executable and select “Properties.”[]Click the “Program” tab.[]Uncheck “Close on Exit.”Click "Okay[/ul](This applies to Win95/98 – I imagine it’s similar for XP, but no guarantees.)

      • No–your program is working right. It is telling the computer to open a DOS screen, print “Hello World”, and then exit–closing the window! It is doing that instantly.
        :smiley:
  • To be able to see your output, you need another line in the function (below the “hellow world” line) that waits for a keypress input–so the window will wait for a keypress, and then close. Get it? I forget how exactly, but look up geyting key input, it isn’t difficult.
    ~

You’re running into a standard “problem” with the command prompt. When it auto-opens to run a program (say, “helloworld.exe”), it also auto-closes when it finishes running. It’s the same way if you do a “ping” or a “tracert” from the “Run” option under the start menu. I’m certain there’s some way to fix this, but I offer an easier solution. If you’re running Windows 9x, go to the start menu and open up the “MS-DOS Prompt.” If it’s NT, 2000, or XP, open up the “Command Prompt.” (Accessories menu if I recall correctly). Navigate to where your program is stored and execute it from the prompt. The window will stay open until you decide to close it yourself.

-Psi Cop

Drat! Beaten not only once, but thrice [grin]. At least two of them were in the same minute as me [smirk].

-Psi Cop

Thanks for the quick reply, works fine now.

If you want a solution that is tied to your program, no matter how is started or how the properties are set you can insert


system("PAUSE");

at the end (or wherever appropriate.) This will give you a windows-language dependent prompt and wait for any key, but it will also do so in a command line environment whether necessary or not.

In the old C days I woulda put in a getch() to pause for a character. Instead you could get something from cin and get the same effect.

I remember having this ‘problem’.
I put a cin command (c++) or scanf © to wait for user input. As others have said your code is doing exactly what it should.

getch() should work fine