Your first C++ Program

You have activated your C++ system, and presumably see a blank window in front of you ready for you to type a program. Try this:

// This is my first C++ program. Here goes nothing ...
#include <iostream.h>
void main ()
    {
        cout << "Hello everybody! This is my first program";
    }

If you can't be bothered to type it, just highlight the text you see above in red by dragging the mouse over it and use Copy and Paste on the Edit menu to copy the text into your C++ system. Remember to include the semicolon (;) after the second set of quotation marks.

Having entered the program, you can compile it by clicking on the appropriate menu option and run it by clicking on another menu option. You should see the message

Hello everybody! This is my first program

appearing on the screen.

How the program works

Here is an analysis of the program, line by line.

Laying out the program

C++ is quite tolerant of the way you lay out the program. All C++ needs is the right symbols in the right order - it's not too fussed about where you put the line breaks in or how many spaces you use. You could miss out all the spaces between keywords if you wanted to or put the whole program on one line. The two examples below are variations on the test program that you have already tried. They are laid out in peculiar ways but they would produce exactly the same results as the program above:

// This is my first C++ program. Here goes nothing ...
#include<iostream.h>void
main(){cout <<"Hello everybody! This is my first program";}

Second example:

// This is my first C++ program. Here goes nothing ...
#include <iostream.h>
void 
main                     ()




{
        
cout 
<<
"Hello everybody! This is my first program";
                                }

There are two places where you aren't allowed to put spaces or line breaks:


Go back
Top of the page
Main Menu
Go on
Go back
Top of the page
Main Menu
Go on