CS 107 - Lab 2

Compiling C++ Programs on Icarus

There are three steps to creating and running a C++ program on the Icarus System.
  1. Creating the source code file.

    We use an editor to create and modify the C++ source code file. The simpliest editor to use is pico. There are other editors on Icarus bnd eventually you will want to stop using pice and use one of these other editors. The two most popular alternatives to pico are vi and emacs.

    The file extension for a C++ file is normally .cpp and this should be used for all files in this course. You may also see C++ files having extension of .C or .cc.

  2. Compiling the source code file

    The compiler that we will use on the Icarus System is the g++ (note: this link is not very helpful). To compile the program source.cpp you type in the following command after the UNIX command prompt.

         g++ source.cpp
    
    Assuming your program has no syntax errors, this will create a file called a.out. This new file is the executable version of the source code file. If your program does contain one or more syntax errors, g++ will give you information about what it thinks is the problem. You will need to edit your program to fix the error(s) before your program will compile and create the executable file.

  3. Running the executable file

    From the UNIX prompt simply type in:

         a.out
    
    This will run the executable program stored in the file a.out.

    Note that the name a.out is the default name for an executable file created by the g++ compiler. You can rename the file or use the -o option with g++ to store the executable file in file with a name other than a.out. To run the executable program stored in a file, type in the name of the file at the UNIX prompt.

Creating Output in a C++ Program

C++ uses the object cout to create output. cout is defined in the library iostream and is attached to the standard output of the machine.

To send information to cout, we use the << operator. An example of this is shown in the following statement:

     cout << "Hello World!";
The characters between the two double quotes will be output by the statement. The semicolon informs the compiler of the end of the statement (just like a period is used to specify the end of a declarative sentence in English).

Output will always stay on the same line unless the \n character (pronounced "backslash-n" or "newline") or the endl object is sent to the output. Thus the following two statements will be on the same line:

     cout << "Hello World!";
     cout << "How are you?";
While the following statements will have each statement output to a separate line.
     cout << "Hello World!" << endl;
     cout << "How are you?" << endl;
The above could also have been written as:
     cout << "Hello World!\n";
     cout << "How are you?\n";
The difference between using \n and endl is pretty subtle that we will not worry about it now. It is suggested that a program always outputs either a \n or endl at the end of the program.

The following is an example of a simple C++ program showing output.

     #include <iostream>

     main()
     {
      cout << "Hello World!" << endl;
     }

Lab Assignment

Due: By your Lab Time during the third week of the semester (the week of 1/27/2003).

You may turn in the assignment to your TA during lab or place it in his mailbox in 905 SEO. It is suggested that you place it in his mailbox just in case you are unable to attend lab.

Create a c++ program on your UIC icarus account that will output the following information. Print out this program and the output created by the program and turn this print out (or print outs) into your TA.

  1. Your name
  2. Your icarus account name (your netid)
  3. Your lab time and day
  4. Your TA's name
  5. The location of the computer lab where your lab will normally be held. Note: this is NOT the room listed in the timetable.
  6. Your major
  7. A favorite joke or quotation.

Note: A number of you wrote this program when doing lab assignment 1. Those people are just ahead of the game.