CMPT 225 Assignment 0, Part a: Hello

Acknowledgement: This is a very slightly revised version of an exercise by Greg Mori.

In this lab exercise we will write a simple C++ program.

Creating your main function

All C++ programs start their execution at the main function. This is similar to Java, though different in that the C++ main function is not contained in a class. We will create a very simple main function that prints "Hello world!", but is slightly more complex than the standard Hello world program. (You can find that program, in more than 300 languages, at the Rosetta Code Hello World page.

These instructions assume you are at a Linux machine in the CSIL lab, although the process should be almost the same on any Linux machine (or OS X, which is also a version of Unix).

  1. You should be logged in.

  2. Open a terminal window. Use the menu icon on the top left of the desktop, and search for "terminal" (or find it under "more apps", "installed").

  3. Type pwd (print working directory) to see which directory you're in. (The string ending in ~$ represents the command prompt, and should not be typed.)

    uname@hostname: ~$ pwd
  4. Change directory to your "sfuhome". Files stored here will be stored permanently and available if you log in to another machine.

    uname@hostname: ~$ cd sfuhome
  5. Make a directory for the files for this lab, for example, call it lab1.

    uname@hostname: ~$ mkdir lab1
  6. Open an editor. Editors available on Linux (in CSIL and elsewhere), include emacs, vim, and gedit. You can open these at the command prompt (e.g., type "emacs&", "vim&", or "gedit&"), or find them in the menu icon interface.

  7. Copy the following code into the editor

    #include <iostream>
    using namespace std; int main (void) { cout << "Hello world!"; return 0; }
  8. Save this to a file called hello.cpp in your lab1 directory.

  9. Change to this directory using cd (change directory):

    uname@hostname: ~$ cd lab1
  10. Type ls (list) to see the contents of the current directory. hello.cpp should appear:

    uname@hostname: ~$ ls

Some explanations

To get input and print output in C++, use the cin and cout functions.  cin is used for input and cout for standard output (to the display).  But first you need the appropriate library.

Include statements

Your program must start with include statements to import the necessary libraries.  The only library you will need for this program is the iostream library which contains the cin and cout functions.  In addition you will need to open the standard namespace to access these functions.  That is the function of these lines above the main function.

#include <iostream>
using namespace std;

Using cin and cout

To get input from the keyboard into a variable named x:

int x = 0;
cin >> x;

To send output to the display:

cout << x;

Note that this will not print the value of x on a new line, but you can use endl to specify a newline.  Below I'm printing the value of x on a new line (with some explanatory text) and then printing another line:

cout << endl << "The value of x is: " << x << endl;

Return values

By convention, main functions return the value 0 if everything went well. Other values can be used to denote various forms of failure or output results.

Compiling your main function

  1. To run the program we just wrote, we need to compile it into an executable. The C++ compiler we will use is called g++. Run the compiler on the program contained in hello.cpp by typing:

    uname@hostname: ~$ g++ hello.cpp
  2. If there are errors in your code, the compiler will let you know. Otherwise, it will produce an executable file called a.out:

    uname@hostname: ~$ ls
    a.out hello.cpp
  3. Run this executable by typing

    uname@hostname: ~$ ./a.out
    Hello world!
    "." refers to the current directory, and "/" characters are used to separate directory and file names in linux/unix.
  4. The name a.out is a default name given to the executable, but is not particularly mnemonic. You can specify another name for the executable produced by g++ using the -o option:

    uname@hostname: ~$ g++ -o hello_world hello.cpp
    You can now run hello_world in a similar fashion:
    uname@hostname: ~$ ./hello_world
    Hello world!

Say hello

Modify hello.cpp to have the following behaviour:


This program was quite simple, but we will illustrate how we might test this program. Download this zip file and save and unzip it in your lab1 directory:
uname@hostname: ~$ unzip
uname@hostname: ~$ ls          a.out         hello_world          hello.cpp
Run the program it contains:
uname@hostname: ~$ ./
If you have correctly modified hello.cpp as specified above, you will see:
Running test 1... 
Running test 2... 
Passed 2 of 2 tests is a Python script, as you might have guessed. The "shebang" (#!) line tells linux to use python to run it. It uses input/output "redirection" (>,<) to pass input (*.in) to your hello_world program and compare (diff) this output (*.out) against specified output (*.gt).

If you have passed 2 of 2 tests, and your program outputs your personal information as described, then collect your grade for the lab by:

  1. Showing your TA that your code compiles and passes the tests (by compiling it and running the tests in from of the TA on your workstation);
  2. Submitting your source code program to the Course Management System
You should aim to complete this exercise before the end of your lab on January 12/15. To claim your grade, you need to have your TA see it and record this fact by the end of your lab on January 19/22, and submit it to coursys by the end of the day January 23.


This exercise will be graded 1/0 (e.g., pass/fail).