Familiarisation Exercises 6-9

Basics of Software Development

Before starting this week's exercises, be sure you have completed all those from last week. If you have had problems with any of last weeks exercises, make sure you get help with them from a demonstrator or practical supervisor at this week's practical.

Familiarisation Exercise 6: Starting the Cygwin environment

Your C++ programming environment is based on an application called Cygwin Bash Shell (or Cygwin), which is provided by Cygnus Solutions. Cygwin is a set of software tools which offers a UNIX (a well-known operating system which has for many years been used on mainframe computers) command-line environment, emulated on your overall Windows desktop.

Before you get started on using Cygwin and its C++ tools, you have to go to the Start button, and then follow the path:
Programs->Accessories->Cygwin->Cygwin Bash Shell
This opens a new window which looks like this:

PowerArchiver 2001

This window offers transparency of files and folders between two interfaces, your newly created UNIX command-line interface within the above window, and your usual Windows icon-based environment. Within the Cygwin window you can access and manipulate your files and folders (view, rename, copy etc.), move from folder to folder, compile and run your C++ programs by typing UNIX-like commands. We do not expect you to learn UNIX commands however (although, learning a bit of UNIX would certainly enrich your "computing power"). You will be provided with a small number of command lines which will be sufficient for your programming needs.

Note: you will need to start this window everytime you log onto a PC and wish to use Cygwin.

In order to witness the above-mentioned transparency do the following:

  1. at the bash$ prompt in the Cygwin Bash Shell window, type the following command:
    pwd
    whose meaning is "print working directory" (term directory in UNIX means exactly the same as term folder in Windows)
    The result of the execution of pwd will be:
    /cygdrive/h
    This means that your current directory (folder) is H:, i.e. Home.
  2. Now, type another command:
    ls
    whose meaning is "list current directory".
    This command prints the contents of your Home directory (folder), including your EEE1004 folder. Compare the effect of this command with the contents of your Home using Windows Explorer.
  3. Now, type another command:
    cd EEE1004
    whose meaning is "change directory to EEE1004"
    (in other words "enter folder EEE1004"). List the contents of this directory, by again typing the ls command. Compare the result with what happens when you click on the EEE1004 folder icon using Windows Explorer.
After the above actions, performed "in step", in the Cygwin window (command-line-based interface), and in Windows Explorer window (icon-based interface), you should be able to grasp the idea of transparency of your access to files and folders between those two different environments.

Clear understanding of this transperency is a key to success in your subsequent mastering of the C++ programming environment.

Familiarisation Exercise 7: Running your first C++ program

Do not panic, you don't have to write a C++ program yet (a little difficult at this stage of your module!), but you are going to compile and execute a program that has already been provided. This will give you some experience of using the Cygwin environment before you have to cope with writing your own programs.

To obtain your first C++ program that you can experiment with:

Having clicked on this link you will see the C++ code displayed in your Internet Explorer window. You now need to save the contents of this Window in a text file, called hello.c in a special folder dedicated for your first C++ program.

Go to the H:\EEE1004 folder using Windows Explorer. In that folder, create a new folder under name hello.

Go back to the Internet Explorer window. Click on File and Save As. In the "Save Web Page" dialogue box, first, inside the "Save in" section reach the newly created folder hello, and click on it. Then, in the "File name" section of the "Save Web Page" dialogue box, type hello, and in the "Save as type" section select Text file. Finally, click on the Save button.

Has it worked? Go into the Explorer window and see if your hello folder contains file hello.txt

Rename this file to hello.c using the following method:

If you have difficulty in renaming it as explained above, using the Windows Explorer, you can rename it by typing the following command in the Cygwin window:
mv hello.c.txt hello.c.

Now make sure that your Cygwin Bash Shell window is open.

Check your current directory by typing command pwd. First, make sure that your current directory is /cygdrive/h/EEE1004. If not, use command cd as you did in the previous exercise. Now, type command ls and see if your current directory contains the name of your new directory hello.

Change your current directory to hello by typing command cd hello. Again, examine the contents of this directory by typing command ls. See if the file named hello.c is there.

At this point you have reached full correspondence (transparency) between your Explorer and your Cygwin interfaces.

You are now ready to compile your first program. Type the following command:
g++ hello.c
(here g++ is the name of a "standard" C++ compiler in the Cygwin environment.

Once the bash $ prompt appears back the compilation process is finished and object code (which is in this case also executable code!) has been generated. The executable code has been stored in a file called a.exe. See if this file has appeared in your hello folder, by both looking in your Explorer window and typing ls command in the Cygwin window.

If the a.exe file is there, the Hello program can now be run by typing command a.exe in the Cygwin window.

When your program runs it must print the following two lines:

Hello Your name
Welcome to EEE1004

in the Cygwin window.

Congratulations! You've just compiled and run your first C++ program.

Familiarisation Exercise 8: Editing your first C++ program

In this exercise, you are going to change the C++ program to personalise it. To edit your program you need a convenient text editor. While there is nothing wrong in using the Notepad editor, which is the one automatically invoked when you double click on the hello.c icon in the hello folder, we recommend you use another editor, called the Programmers File Editor. We believe this editor is more user-friendly than Notepad.

  1. First, locate the Programmers File Editor. From the Start button, follow the path: Programs -> Accessories -> Programmers File Editor.
  2. Click on this selection. Go to the File menu and select Open. Make sure that you see your files in the hello folder. If necessary navigate via the "Look in" box to the hello folder.
  3. Double-click on the hello.c icon in the main area of the "Open Existing File" window. This will bring up an editor window (named hello.c) in which the C++ source code of the program is displayed. You are not expected to understand all of the C++ gobbledegook yet, but if you scroll down the window you will be able to find the text strings that were displayed in the Cygwin window when you ran your program after compiling it first.
  4. Change the text string, saying "Your name", enclosed by " and " so that your name is printed out, together with the module number. Be careful only to change the characters inside the double quotes. The double quotes themselves must remain.
  5. Go to the File menu and save the file under the same name hello.c
  6. Return back to your Cygwin Bash Shell window.
  7. Examine the contents of the current directory by typing command ls. See if the file named hello.c is there. If this file is now named hello.c.txt, rename it to hello.c by typing command mv hello.c.txt hello.c.
  8. Then type again: g++ hello.c
    to start the compiler.
  9. When the compilation is finished, type a.exe and observe the result. Your name should now appear in the greeting!

Here are a couple of other things you can try with your Hello project. Make the following modifications to the program hello.c, and for each modification, predict what the output from the program will be. Then re-compile and re-run your hello program for each modification.

When you have finished, use the File menu to exit the Programmers File Editor. Then type command exit in the Cygwin window.

Familiarisation Exercise 9: Slightly more complex program

Try to experiment with another C++ program, called newhello.c, which is a bit more complex version of a greeting program.

This program, as you can see, includes a definition of an object, called name, whose type is string.

Save this code in a file newhello.c in the same folder/directory hello.

First, use command g++ newhello.c to compile it. Run the new program with a.exe and see how it works. It should involve you in a dialogue.

Before logging out, make sure that you exit the Cygwin Bash Shell and all other applications.

Alex.Yakovlev at newcastle.ac.uk
1 October 2007