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.
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.cppAssuming 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.
From the UNIX prompt simply type in:
a.outThis 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.
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; }
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.
Note: A number of you wrote this program when doing lab assignment 1. Those people are just ahead of the game.