Hey everybody! Im new to C++ and I have no experience with any other programming language. I downloaded a compiler from bloodshed.net and tried programming the 'hello world' program:


/*Hello world program.
Seth Koberg 02/09/05 */

#include <iostream.h>

int main()
{
    cout<<"Hello World!";
    return(0);
}

When I run the program, it terminates right after it opens up. I dont know why its doing this. Anybody have any thoughts on how I can fix this? Thanks

Dani AI

Generated

A quick diagnosis and practical fixes.

As explains, the program is ending normally and the console started by the IDE closes as soon as the process exits. A reliable way to confirm this is to run the compiled executable from an OS command prompt (open a shell, change to the folder containing the .exe, run it) so the window stays open and any error messages remain visible. Also check the compiler/build output to make sure the program actually compiled and produced an executable before running anything.

Prefer a portable, standard pause instead of calling an external shell command. Waiting for input keeps behavior consistent across platforms and avoids invoking OS-specific tools. A common pattern is to prompt and wait for Enter, for example:

std::cout << "Press Enter to exit...";
std::cin.get();

This uses the C++ standard library and does not rely on Windows-only commands. For flushing and visibility of the printed message before waiting, end the line with std::endl or use std::flush.

About headers and namespaces: the old iostream.h form is legacy. Modern C++ uses <iostream> and its symbols live in the std namespace. suggested using namespace std; and warned about it; for tiny examples it is acceptable, but the safer practice is to either qualify with std:: or import only specific names (for example using std::cout;) as projects grow.

Checklist summary (what the thread so far has not fully shown): confirm a successful build and the executable location, run the exe from a command prompt to observe output, use a standard input-wait (std::cin.get() or std::getline) to pause, and prefer <iostream> with std::-qualified names for portability and modern style.

Recommended Answers

All 4 Replies

You program simply ended after the output line. I assume that you were running it directly from the dev environment. If you run it from a cmd shell you will see the output:

Hello World

To add a pause, try the following code:

#include <iostream> // io classes etc
#include <cstdlib> // system()

using std::cout;

int main()
{
cout<<"Hello World!";

system( "pause" );
return 0;
}


Note that the #includes do not have a .h extension. Modern C++ does not use the extension, it remains for legacy systems.

The line system("pause") simply calls the pause command. It is not a very efficient way to pause a program, but for simple command line apps it is quick & easy to use.

Also modern C++ puts the standard library (which includes cout) in the std namespace. You have to tell the compiler that you are using the cout in the standard library. Thats what the 'using std::cout;' line does.

Try using

using namespace std;

just after the headers.

using everything in a namespace is not a great idea in general, as it negates the benefits that namespaces bring in the 1st place.

It is far better paractice to only include those symbols that you definitely want.

>It is far better paractice to only include those symbols that you definitely want.
Yes, though for such a small program it really doesn't matter. Namespaces don't become useful until you start working on projects with code from multiple sources (such as third party libraries). Up to that point, namespaces are just a nuisance.

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.