2. So… How Do I Use and Code in C++

In essence, you give instructions (write code) to a C++ compiler that processes and does stuff (e.g. do calculations, give output to user, take user input) based on your instructions.  A C++ compiler is something that can be downloaded onto your computer (such as apps like Microsoft Word). If you type statements in a certain way (code in C++ language), when you run the code, the compiler can execute your instructions. However, we will use an online C++ compiler (click here)[1]  - this has the same basic abilities as a downloaded compiler, but is easier to use. Look at the picture below - when you open the compiler you should see something like this (without the annotations). 

First, you should see that on the top, there is a “run” button. When you are done coding in the editor section, click the run button so your code (instructions) are executed. On the top right, the language should be C++ (the one we are learning).

You can try typing things into the editor (the place where you type code or text) to get a feel for it. On the left, you can see numbers which represent the line number - this is useful if you have long code and you need to work with someone else on it. For example, when doing a coding project with a partner, it is much easier to say “look at line 1001” than to say “find the place where I coded XYZ”. 

Most basic C++ codes (the ones we will work on) have two sections (the two green boxes in the figure). The first section tells the compiler what coding conventions and libraries you are using. Because C++ is used for many types of projects, people need C++ to do different sorts of things - for example, some want to use C++ for math for statistics calculations while others want to use it for text input/output (letters and words). Some functions are useful for some people, but useless for others. Thus, to prevent C++ from having all of these functions built in, we need to tell the compiler which sets we intend on using. This is done by including libraries that fit our needs - to include a library, you need to type something like:

#include <Library Name>

Note that the “<” and “>” are needed. This can also be seen in line 9 above - the library name we include there is iostream[2], which is a library that takes in user input (you can interact with the program after it compiles by typing in commands, values or input) and output (after the code compiles. More on this later…

On line 11, using namespace std is another ‘notification’ to the compiler that we will be typing things in a ‘standard’ (hence std) way. In most cases, you don’t need to change it or worry about it.

Lines 9 to 11 form the first part of the code (telling C++ your conventions and libraries). Lines 13 to 18 form the second part of the code (the part where you actually code that gives the compiler instructions on what the program should do). Although we know that the second part of the code starts from line 13 onwards, the compiler is not as smart as us. It does not know this unless we say it explicitly. 

Thus, the main code always needs to start with int main() as on line 13 - this is a signal to the compiler that the main code is about to begin. Then, the compiler knows exactly where the main code is by looking for the curly brackets { and }. The compiler will assume that your official code (instructions) start after the open bracket { and ends when you write the close bracket }. In this case, that means that our main code (instructions/important part) starts at line 14 and ends at line 18 (i.e. lines 15 to 17 are the main code). 

Another important detail is that before the close bracket of the main (line 18), we always need to ‘return’ something (it says return 0 on line 17) - this is not really part of your instructions to the compiler, but without it, the code won’t run. The C++ compiler runs your code line by line (from 1 to the end) - the return 0 at the end means that if nothing bad happens and the code can execute, an “error code” of 0 will pop up after you run the code, meaning that there is no error.

The code in this example is relatively simple in terms of the instructions it gives. On line 15, it says cout<<”Hello World”; . What do you think this does? Try clicking the run button on the top of the screen and seeing what happens. After the program compiles, you should see something like this at the bottom of your screen in the console: 

As you can see, it printed  “Hello World” to the console (the part that pops up after your program compiles).


Test your knowledge:

Try to modify the code in line 15 so that the code prints something else, like your name. In this case, line 15 was the ‘instruction’ we gave to the compiler: print two words out onto the console. Usually, instructions are more complicated (such as performing calculations, comparing numbers etc.) and take up more than one line - this will be explained in the next section.

1 onlinegdb.com2 It stands for input-output stream. It allows us to use cin and cout