1.6 Coding, Compiling and Running a C++ Program


Layout of a C++ program

Programs are written using programming languages. There are many programming languages ranging from the earlier languages such as FOTRAN, COBOL and Pascal, to more recent languages such as C++, Java, Visual Basic, ASP, PHP and so on. These languages are called high-level languages and they resemble English language. They are designed that way so that it would be easy for humans to read and write in these languages.

Besides high-level languages, there are also low-level languages and machine language. You can try and find out more about these languages yourself. In this course, we will concentrate on the C++ language.

Once you have decided which language you are going to use to write and later run your program, you must learn the “rules” or the “grammar” of using the language. First of all, you must know that each language must be written or coded in a specific way, or a specific layout. There are some specific “statements” which you must have in your program and it must be typed exactly as the language requires it. Let’s look at an example of a C++ program. This program is a simple program and I will not design the algorithm for it. This program will display the word “HELLO” on the screen. The program looks like this :


1.6 Coding, Compiling and Running a C++ Program





1.6 Coding, Compiling and Running a C++ Program


This program codes can be typed or edited in any text editor such as a Notepad. However, you can also use any C++ software development kit which you purchase or download. The kit which contains both the editor and compiler in one integrated environment is called an IDE or Integrated Development Environment. Examples of these kits are Microsoft Visual Studio, Dev C ++, among others. There is one thing that you should be aware of, which is, each IDE may have it’s own “rules”, and therefore, the examples given here may have to be adapted to run on the IDE you are using.

Now, let’s try to understand the Hello program given above. To make it easier to refer to the program statements, I will place the line number for each of the line in the program codes. The program code can be saved as a file and we usually refer to these codes as source codes.


1.6 Coding, Compiling and Running a C++ Program





1.6 Coding, Compiling and Running a C++ Program


Explanation of the C++ codes :

Lines 1 and 2 are comment lines. This is indicated by the usage of the /* and */ symbols. The /* symbol indicates the beginning of a comment and the */ symbol indicates the end of the comment line. Lines 3 and 6 were intentionally left blank to add readability to the program codes. They do not have any effect on the execution of the program.

Line 4, has the #include directive. Lines which start with “#” are called pre-processor directives. The #include directive has a specific job and it must be present in your C++ program. It tells the compiler to include the contents of a particular file in the current program. At this point of time, you only need to use the directive.

#include <iostream>

What this directive does, is to ask the compiler to include the contents of the iostream file. The iostream file must be included in any program that uses input/output statements such as cin statement to input data from keyboard, and cout statement to output data to the screen. C++ is a case sentitive. Which means, if you type #Include (with a capital letter “I”), it will create an error in your program. This is called a syntax error, which means the program (or more accurately, the compiler) does not recognize the word “Include”.

1.6 Coding, Compiling and Running a C++ Program


Note: You may find that in some C++ environment, the file is specified as iostream.h instead of iostream.. If the environment requires this, you can replace lines 4 and line 5 with the following statement

#include <iostream.h>

In many cases, you are not to include line 5. You can look at sample programs provide by the IDE to see which style it requires.

Line 7 has the beginning of the program, which is the statement

int main()

The word main() is the name of a function. A function is a block of codes placed between the curly brackets { and }. You know that a word is a function if it has () following it, such as main(). We will discuss more about functions later. In many cases, the function main() is the beginning of the C++ program, therefore this function is required in C++ programs. The word int preceding the word main indicates that the function main returns an integer number. It is also possible to have a void main() statement to indicate that the main function does not return any value. Actually the int main() statement is called a function header. It is the beginning of a function. A program can have only one main() function. However, there may be other additional functions in a C++ program, as we will find out later.

1.6 Coding, Compiling and Running a C++ Program


The main() function is followed by the body of the function. The beginning of the body of the function is indicated by the curly bracket {, and the end of the body of the function is indicated by the curly bracket }. Lines 8 and 12 show these symbols respectively. It is in the body of the main() function where the logic or the algorithm of the program are located. In the program shown above, all the program will do is to print the word “HELLO” on the display monitor. This is done by using the cout statement as shown in line 9.

Line 11 contains the statement to complement the int main() statement at the beginning of the program. This return statement simply return the value “0” to the operating system at the end of the program. Value 0 indicates that the program has terminated successfully. We will elaborate this return statement when we discuss functions in later chapters.


1.6 Coding, Compiling and Running a C++ Program


The figure below summarizes the important points mentioned in the above paragraph :

Draw diagram similar to pg 26 of Ivor’s book

Figure 1

1.6 Coding, Compiling and Running a C++ Program


1.6.2 Coding a C++ program

Once we have determined the codes for the program, how do we type them into the program and run them? For this course, we are going to use the Dev C++ IDE which can be downloaded from the internet.

After you have downloaded the software, you can open the editor which is shown in the following figure. You can also use other C++ IDE as mentioned earlier.



1.6 Coding, Compiling and Running a C++ Program


You can open a new source file where you will type the source codes given in the HELLO program above. Figure 3 illustrates.



1.6 Coding, Compiling and Running a C++ Program


1.6.3 Compiling and running C++ program

Once you have typed in the codes, you need to compile the codes. To ensure that you don’t lose your codes, you should save the codes before compiling them. In some IDEs, the word “build” is used instead of compile. What this process does is for the system to read your source code and understand it. What happens is that the system compiles the source codes into object code, and a linker links the object code to machine code required for the program to run. If you look in the directory where you saved your source code, you will find new files which have been created by the system. The final step is to run or execute the compiled codes.

If your codes did not follow the correct syntax rules, the compiler will identify the errors. However, it does not guarantee that the program will run correctly. During execution, the program may terminate due to errors in the algorithm, and this type of errors is referred as logical error. Another problem you may encounter is that even though the program does not terminate, it could still provide incorrect output, and this is also caused by error in the algorithm. In cases like these, you will have to re examine your codes, your algorithm, and maybe even your problem analysis.

For the above program, the compilation of the codes should be successful and will show the following response.

1.6 Coding, Compiling and Running a C++ Program




1.6 Coding, Compiling and Running a C++ Program


Once the compilation is successful, you can execute the program, and the result will be as in the figure below.



1.6 Coding, Compiling and Running a C++ Program


1.6.4 More Practice

You can now try to type in the codes for the problem which we have analyzed earlier.

Without trying to understand the whole program at this point of time, the following is the codes that would be written for the currency conversion problem stated earlier.



1.6 Coding, Compiling and Running a C++ Program


After keying in the source codes into the IDE editor, you should save it in a file. Then compile the source codes. If you get any error during compilation, check that you have typed in each line of code correctly. If your compilation is successful, you can execute the program. In this program, when the program is executed, it requires the value od USD and conversion rate as inputs. You can use the values that we have determined earlier for the desk-checking process, and once the values are entered, the program will display the value in RM. The following figure illustrates the output of the program using the two sets of input values given.



1.6 Coding, Compiling and Running a C++ Program