C++ Basics 1 : A simple first program in C++

If you've followed the tutorial on creating a new project in Visual Studio or in Dev-C++ then you'll be ready to write a program! This tutorial takes you through the steps of creating your first program in C++.

Right now you should have a big white empty space on your screen, waiting for your code.

Before you start: using comments

See the mini-tutorial about commenting your code before you start - it's an essential habit to get into so you might want to start now so that you can comment your first program as you go along.

Include the required libraries

Type the following command:

#include <iostream>

This command tells the compiler to find a library called iostream and include it in your program. A library is a set of functions that another programmer has already written to save you the job. In this case the iostream library allows you to get input from the user and put output on the screen.

Define the namespace

Hit The RETURN key a couple of times and then type the following command:

using namespace std;

Many of the commands that you will use in C++ are hidden away in classes - which means that to use the command you need to know the name of the class it belongs in.

To make life easier for you, you can specify the 'namespace' - enabling you to use the command without its object name as long as you've specified the namespace in which it belongs.

In this case you will be using two commands called cout and endl. Their full names are std::cout and std::endl but by using the line above you can refer to them as just cout and endl.

Create the main function

Hit The RETURN key a couple of times and type the following:

void main(void){

Programs in C++ are broken up into smaller blocks of code called functions. Each of these blocks must have a unique name so that they can be referred to later.

When a C++ program is run, it starts in a block of code called main. If that function isn't there your program won't run. The line above creates that function.

Write some code in your main function

Press The RETURN key after the curly brace (if you haven't already done so) and type the following:

cout << "Hello world" << endl;

The command cout means "console output". It enables you to output something to the computer screen by using a stream of characters that flow from your data, through cout, onto the screen.

Following the command you must have what is known as the stream operator. Think of this as a connector that connects what you want to output to the cout command.

Notice how the operator looks like an arrow that flows the words "Hello world" to the console output. That direction of flow is important. The data that you want to output must 'flow' towards the cout command.

Add a command to pause the program

Press The RETURN key and type the following:

system("PAUSE");

This is a command that is commonly used to display a "Press a key" message to the user that then pauses your program until the user presses a key.

Unfortunately this command relies upon a system command called PAUSE. This means that if you use this step, your program code might not be 100% cross-platform compatible. Another system (e.g. a small mobile device) may not have the PAUSE command as part of its operating system.

Normally when you run your program, the command window is displayed (also known as the DOS box) and your program output is displayed in that box. Unfortuantely when your program is complete, that box is closed. This command gives you the chance to see your program's output.

Add the command that returns the function's output value

Press The RETURN key and add the following line of code:

return EXIT_SUCCESS;

Your main function should always return an integer value to indicate to the system (or another application that ran it) if the program completed successfully or not. The label EXIT_SUCCESS is actually a number. When your program is compiled the words will be replaced with the number zero, meaning that your program is ending normally (as opposed to ending early due to an error).

Close the main function

Press The RETURN key after the last command (if you haven't already) and type the following character:

}

That single curly brace signifies the end of the main function. In this case it's also the end of your first program!

Running or building the program in Visual studio

To run the program with a "press a key" prompt at the end:

Ctrl keyF5 key If you're using Visual Studio, hold down Ctrl and press F5 to run it. In the example code in this tutorial you won't need to do this because the system("PAUSE") command will ask for a key to be pressed anyway.

To run the program without a "press a key" prompt at the end:

F5 keyPressing F5 on its own also builds and runs your program but it doesn't ask the user to press a key at the end - which means you won't get to see the output of your program.

To just compile the program without running:

Ctrl keyYou might want to just compile your program to test for syntax errors. To do this you just need to press  Ctrl+F7.

Running or compiling the program using Dev-C++

Press F9Press F9 to compile and then run the program.

Hold down CtrlHold down the previous key and Press F9If you don't want to run it, you can compile the program (to see if there's any errors in it, perhaps) by pressing CTRL+F9.

The output of the first C++ program

Complete program, including comments

// Include the input/output stream library

#include <iostream>  

// Indicate that commands are being used from the std namespace

using namespace std;  

// Begin the main function, specifying that
// there is an integer as the output but no inputs

int main(void){  

// Stream the words "Hello world" followed by
// an end of line character to the screen

cout << "Hello world" << endl;  
 

// Use the system command PAUSE to ask the user to press a key

system("PAUSE");
 

// Use the special code EXIT_SUCCESS as the return
// value for the main function. This means everything went OK.

return EXIT_SUCCESS;
 

} // End the main function

Exercise

To practice what you've learned in this tutorial you might want to try this exercise.

  1. Change the contents of the main function so that, instead of outputting "Hello World" it outputs the colours of the rainbow on seperate lines. In case you're not sure, the colours are: red, orange, yellow, green, blue, indigo, violet. Click here for a solution.