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.
Here is an analysis of the program, line by line.
This is a comment. Comments are just for the benefit of any human reading the program. The compiler ignores them. Although rather pointless in a short program like this, they are useful when the program becomes long and complicated. It is quite common for a person to write a program and then come back to it a few days/weeks/months later and not understand how it works. Make every stage of the program obvious with comments.
The simplest way to put in a comment is by starting the line with // as anything after this is ignored by the compiler. The comment doesn't have to start at the beginning of the line. You can put the // symbol after some proper computer instructions.
for (x=1;x>10;x++) // This is the main loop
Here the comment comes after the for instruction.
This is an instruction to the compiler to include a library of commands. C++ tends to "farm out" various parts of itself and store them in different files. For instance, the file iostream.h contains everything that C++ needs to know about input and output (printing things out on the screen and getting input from the keyboard). If you want your program to do input or output, you must tell C++ to include the appropriate instructions to do it. Since most computer programs would be pretty useless without input or output, you will need this instruction in just about every program you write.
Why is C++ arranged like this? Different computer systems will have different ways of displaying items on the screen and getting input from devices such as the keyboard. If the parts of the program which are system-specific are stored in separate files, then it allows the same C++ program to be compiled, virtually without change, on many different machines.
All C++ programs must include a line with the word main in it. C++ is a language based on small sections of code called procedures and the main part of the program is contained in the main procedure. This is the statement telling the compiler that it is the start of the main procedure.
The word void indicates that the main procedure does not return a number. Procedures can produce numbers. For instance, a procedure to work out the average of a set of numbers would return that average.
Programs consist of program statements, which are the individual instructions to the computer. Sometimes you will need to group these statements into blocks, which are connected in some way.
The symbol { is used to start a block of statements, and } is used to end it. Those of you with slightly dodgy vision should notice that these are curly brackets (to the right of the P key on the keyboard), not round brackets (the ones on the "9" and "0" keys on the top row). If you put in the wrong sort of brackets at the wrong time then the compiler will inform you of it.
This is the main part of the whole program. The instruction cout is found in the iostream.h file (hence the need for the include statement above). It prints things out on the screen.
The << symbol is a way of passing something from one item in a program to another and the words within quotation marks are the thing that is passed to the cout instruction.
Any item within quotation marks is called a string. It represents a sequence of letters, spaces, punctuation symbols - plain English text basically. In this case, the string is passed to cout which prints it on the screen.
You must include the semicolon (;) at the end for reasons which will become clear in the next section.
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:
vo id mai n
cout << "This would cause an error when you compiled it.";