How to test the code examples?

Many code examples are presented in this tutorial notes and you are invited to try the examples, modify them, etc. To this end, you can simply copy and paste the presented C++ code snippets into the editor of your favorite IDE system installed in your computer (e.g. Visual Studio or Xcode). You can then modify and\or compile and execute the code.

Another popular alternative is to use an online C++ compiler.

Online C++ compilers

An online compiler system is simply a webpage that offers the possibility to edit C++ code and compile it directly from the webpage. The use of online compilers offers the following possibilities in a handy way.

  • To compile and run small C++-programs without the need to use (and install ) an IDE plus compiler in your machine.
  • To compile code with different compilers which increases the chance to find out problems with code that one single compiler may not find.

Most programmers use online compilers and find them very useful. To encourage you to experiment with the code examples presented in this tutorial notes, many of the examples have a clickable button named “Run Code” below it which allows you to use an online compiler to compile directly the code in the presented example and runt it. It’s also possible to modify the given code, if you wish so. The online compiler system used is Coliru, the same online compiler used in the reference manual of the C++ language, cppreference.com. Clang is the C++ compiler used by Coliru to compile the C++ code.

There are other C++ online compiler systems, besides Coliru, which offer different types of services to programmers. Some examples of other online compiler systems are given below.

Of course you don’t have to use Coliru or any other C++ online compiler to test the presented example C++ programs. If you prefer, you can use your favorite IDE (e.g. Visual Studio or Xcode), and simply copy and paste the code in the editor of your selected IDE.

Coliru

To use the online compiler system Coliru is simple. We give here a brief introduction, so that you can easily test the examples presented in this website.

You can find below a simple C++ that displays “Hello!!”.

1
2
3
4
5
#include <iostream>

int main() {
    std::cout << "Hello!!\n";
}

If you click on the button “Run Code” below the program then the program is loaded by Coliru and a new window opens with a content similar to the figure below. In this window, Coliru shows the program and, at the bottom of the page, there is a line with the command to call the compiler (which you can ignore by now), followed by the output produced by executing the program.

Coliru

In the next example, we have a C++ program that requires the user to enter two integers. If you click on the button “Run Code” then you won’t be able to enter the input (the two intgers). The program simply executes by giving some value to the variables i1 and i2 (like zero) and then the output “0 + 0 = 0” is displayed. Not very useful, of course. So, let us see what we need to do to be able to enter input for the programs when using Coliru system.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main() {   
    std::cout << "Enter an integer: ";
    int i1;
    std::cin >> i1;
	  
    std::cout << "\nEnter another integer: ";
    int i2;
    std::cin >> i2;
	
    std::cout << "\n" << i1 << " + " << i2 << " = " << i1+i2 << "\n";
}
  • Click on the button “Run Code” so that a window opens with the program loaded in Coliru.
  • Click on the button “Edit”, on the bottom right corner of the Coliru window.
  • On the line with the compiler information, after “a.out”, add <<< followed by all input required for the program, between single quotes and separated by white spaces (<<< 'input1 input2'). See the next image showing the integers $10$ and $-8$ passed as input to the program above ($10$ and $-8$ will be stored in the variables i1 and i2, respectively).
  • Finally, click on the button “Compile, link and run…”.

Note that you need to click on the button “Edit”, if you want to modify the program’s code.

Coliru

When you click on the button “Compile, link and run…” the program is compiled and executed (if no compilation errors are detected). The output is then shown below the program’s code.

Coliru