Thursday, November 1, 2012

CPP 02 - Displaying a Text on the Screen

Our first program will display a text on the screen. In our examples, your program will use command line interface (yes, Maggie, it is this 'black thing' that should show us the results upon a source code compilation and binary code execution).

Type in the code presented in Pic. 2.1. It should be the exact copy of what you see below (use TAB key to create indentation. Do not type in the numbers on the left (this rule will apply to all our coding).
Pic. 2.1 – ex002.cpp

Source Code:

#inlcude <iostream>

using namespace std;

int main() {

cout << "\nHello World!\n";

return 0;

In CodeBlocks IDE software, save the project and compilie/execute by clicking the appropriate icon in the menu.

In Linux, the compilation of your source code above is going to be:

$ g++  -o ex2  ex002.cpp

In order to run the binary file created after compilation you would run it this way (in Linux of course):

$ ./ex2

The result will be something similar to mine (highlighted part):

Pic. 2.2 - Hello Displayed on the Screen.


The statement 'cout << ' expect you to type in a string (string= bunch of characters, letters, number etc.) in double quotes " ". The content between these double quotes will be displayed on the screen.

Pay attention to special characters I have used in this short program:
'\n' - means create 'new line', the equivalent would be cout << endl;

You could use a tabulator this way by using '\t' characters.


Exercise 2.1
Create a program that displays the following on the screen (highlighted part):

Pic. 2.3 - Homework Exercise 2.1 Result.

Exercise 2.2
Using tabulator, create a program that displays the following on the screen (highlighted part):

Pic. 2.4 - Homework Exercise 2.2 - Result.