Hands-on 1b

First C++ program

Start code: session1-start.tar.gz

Construction of a simple application

In this step we will learn the cycle of developing an application:

  • Defining a working environment
  • Editing
  • Compilation
  • Execution and tests

The above steps represent a typical cycle of software development. While the step 1 is done just once the steps 2 - 4 are usually repeated many times when developing software.

Defining a working environment

Create directory Project, where we will put all our exercises.

$ cd 
$ mkdir Project
$ cd Project

Editing

  • Download the start code (see the link on the top of this page) and unpack it in your working area (directory Project).
$ tar xzvf session1-start.tar.gz

This will create a new directory session1-start containing just one file, the program hello.cxx. Inspect the file content in your editor:

#include <iostream>

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

Compilation

Build the application using the C++ compiler, in our case g++:

$ g++ hello.cxx

The result of the compilation appears in the file a.out:

$ ls
a.out     hello.cxx

To give our application the name hello, we will change the previous command by adding the option -o:

$ g++ -o hello hello.cxx 
$ ls

We will also activate printing of “all” warning messages by adding the option -Wall:

$ g++ -Wall -o hello hello.cxx 

To compile code which includes new features of C++ introduced in the C++11 standard, we will need to use the option -std=c++11.

$ g++ -std=c++11 -Wall -o hello hello.cxx

Execution and tests

We can now test the application:

$ ./hello
Hello!

Familiarization with C++

We will now change our program step by step in order to get the behavior described below.

We let the program stop after printing "Continue ?" and wait for an input from the keyboard:

$ ./hello
Hello!
Continue ?

The program will repeat printing when the user types "yes":

$ ./hello
Hello!
Continue ? yes
Hello!
Continue ? yes

and stop execution when the user types "no":

$ ./hello
Hello!
Continue ? yes
Hello!
Continue ? yes
Hello!
Continue ? yes
Hello!
Continue ? no
Goodbye.
$

Now you are ready to start to modify your program following the instructions below step by step.

Step 1 Printing on the screen

In the first step, we will study in detail the program provided.

#include <iostream>

using namespace std;

int main ()
{
  cout << "Hello world!" << endl;
}
  • In order to use standard input/output we have to include the <iostream> header.
  • The directive using namespace std; makes all the symbols from the standard library accessible without qualifying them with std::.
  • cout represents the standard output stream object. It uses the << operator to write on the screen.
  • The manipulator endl writes a line return (which must be specified and is not automatic).

Step 2: Reading from the keyboard

In the next step, we will extend the program to read a string of characters from the keyboard and print the values on the screen.

// ... 
int main ()
{
  cout << "Hello world!" << endl;

  // Read in a string of characters from the keyboard
  string answer;
  cin >> answer;
  // Write on the screen the value which was read in
  cout << "You typed in: "  << answer << endl;
}
  • The comment lines // ... represent the code which is already in place and has been discussed.
  • Using a variable answer for strings (defined in <string> header)
  • cin represents the standard input stream object. It uses the operator >> to read from the keyboard.

We compile and test:

$ g++ -Wall -o hello hello.cxx
$ ./hello

Step 3: Loop

Now we add a loop:

// ... 
int main ()
{
  // ... welcome message

  string answer;
  do {
     // Print a question "Continue?" here and read in the 'answer'
     // from the keyboard
  }
  while ( answer == "yes" || answer == "Yes" );
}
  • The operator comparison in C++ is ==;
  • The logical operators and and or are respectively && and ||.
  • The result of the comparison is of bool type, and whose values are true or false.

We compile and test:

$ g++ -Wall -o hello hello.cxx
$ ./hello