Home / code / cpp

C++ the basic

Note I have learned to write little dos-box programs. Everything mentioned here is focused on that.

We start with an example:

//example My first c++-program
#include <iostream.h>

void main()
{
  cout << "Hello World";
  cin.get();
}

The only thing this program does, is write Hello World. You end the program by pressing any key.

The first line is comment. The 2 slashes tell the compiler that it is comment. The // just comment 1 line out. If you want to comment more line use /* to open and */ to close.

The second line #include <iostream.h>, includes a library. There are many different libraries. But for now iostream.h will do. The iostream library contains function to get input from the keyboard (cin) and write to screen(cout).

void main(): This is the start of the main function. In each program there must be a Main function. The contents of this function is between {}.

cout << "Hello World";: This line writes Hello World on the screen. If you want to but something on the screen you always use cout.

cin.get();: A simple line to make sure the program doesn't close immediately. It waits for a key to be pressed.

As you can see every line ends with ; (except the start of the function). This needs to be done so the compiler knows that a new line is started.

Note:
If for some reason you can't use the library iostream.h, try to use stdio.hinstead.
And use fprintf(stderr,"") instead of cout << ""

 

TOP

Latest script:

 

Books: