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!";

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

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.