Lab 01
Lab 01
Lab Exercises
LAB 1.1 – TRY IT: Compiling and Running Your First Program
Step 1: Add the greeting.cpp program from your Lab1 folder to the project. Your instructor will
show you how to do this. Here is a copy of the source code.
01 // greeting.cpp
02 // This program prints a message to greet the user.
03 #include <iostream> // Needed to do C++ I/O
04 #include <string> // Needed by some compilers to use strings
05 using namespace std;
06
07 int main ()
08 {
09 string name; // This declares a variable to
10 // hold the user's name
11 // Get the user's name
12 cout << "Please enter your first name: ";
13 cin >> name;
14
15 // Print the greeting
16 cout << "Hello, " << name << "." << endl;
17
18 return 0;
19 }
Step 2: Read the source code. What do you think the program will display when it is run if the user
enters the name Adam on line 12? _________________________________________________
Step 3: Follow the instructions your instructor gives you to compile the program. You should see a
message telling you the program has compiled correctly. This message will be different for each
compiler, but may look something like this:
1
TCP1101 Programming Fundamentals
Step 4: Now follow the instructions your instructor gives you to execute the program. Enter the name
Adam when the program prompts you for a name. Did you get the output you expected? ________
Run the program again, and this time enter your first name when you are prompted for a name. Write a
copy of the output here. ______________________________________________________
The greeting.cpp program compiled and ran correctly because it contained no errors. However,
there are three different types of errors that can occur in a computer program, and all of these need to be
found and fixed before a program will work correctly. Each of these three errors is examined below.
A syntax error in a program, just like a syntax error in English, means that a grammar rule has been
broken. One or more of the programming statements in the source code do not follow the rules for how a
C++ program must be written. Let’s take a look at the most commonly made C++ syntax error, an
omitted semi-colon.
Step 1: Remove the semi-colon on line 12 of the greeting.cpp program. To do this you can simply
place your cursor at the end of line 12 and press the back-space key. Once the semi-colon is gone,
recompile the program and look at the error message displayed at the bottom of the screen. The exact
message you get will depend on which compiler you are using, but it will likely look something like this:
Always read the message carefully. Sometimes it is clear what is wrong. Other times the message may
be confusing to a new programmer, but you will get better at deciphering compiler error messages as
you gain more experience. This message is quite clear. It says that a semi-colon is missing before the
word cin.
Notice that the compiler error message also includes a line number indicating the approximate, but not
exact, location where the error occurred. In this case the error occurred at the end of line 12, but the
compiler message reports the error as occurring on line 13. This is because the compiler did not detect
the problem until it reached the cin on line 13. Any time you get a compiler error message and do not
see a problem in the line reported, try checking the previous line.
Step 2: A program containing compiler errors cannot be run until the errors are fixed and the program is
recompiled. Put the semi-colon back in at the end of line 12 and then compile the program again. If you
have done it correctly, it will compile with no errors this time. Now you can run the program again.
Step 3: Follow the instructions your instructor gives you to remove greeting.cpp from the project.
This must be done so the same project can be used to run a different program.
2
TCP1101 Programming Fundamentals
A run-time error occurs when a program instruction tells the program to do something it is unable to
correctly do. This type of error cannot be detected by a compiler because no syntax rules have been
broken. So the program will compile but, as the name suggests, something will go wrong when the
program is run. In some cases, a run-time error causes the program to abort; in others it continues
running but produces incorrect results. Let’s take a look at a common run-time error, attempting to
divide by zero.
Step 1: Add the average.cpp program from your Lab1 folder to the project. Here is a copy of the
source code.
01 // average.cpp
02 // This program finds the average of two numbers.
03 // It contains two errors that must be fixed.
04 #include <iostream>
05 using namespace std;
06
07 int main ()
08 {
09 int size = 0; // The number of values to be averaged
10 double num1,
11 num2,
12 average; // Average of num1 and num2
13
14 // Get the two numbers
15 cout << "Enter two numbers separated by one or more spaces: ";
16 cin >> num1 >> num2;
17
18 // Calculate the average
19 average = num1 + num2 / size;
20
21 // Display the average
22 cout << "The average of the two numbers is: " << average << endl;
23 return 0;
24 }
Step 2: Read through the source code to see if you can spot any errors in the program. Two lines
contain errors, but even if you can find them, do not fix them yet.
Step 3: Compile the program. Since it contains no syntax errors, the compiler should not detect any
errors.
Step 4: Run the program and, at the prompt, enter 10 5. Did the run-time error cause the program to
abort or to produce incorrect results? If it ran without aborting it probably displayed something like this:
3
TCP1101 Programming Fundamentals
Step 5: The error occurs on line 19 when a quantity is divided by size. Notice that size is set in line
9 to 0. Correct the error by changing line 9 of the source code to set size equal to 2 so that the
quantity on line 19 will be divided by 2. Then recompile and rerun the program, again entering 10 and
5 for the two numbers. The output should now look like this:
Step 2: Recompile and rerun the program, again entering 10 and 5 at the prompt. Now that you have
corrected the logic error, you should get the following correct result.
4
TCP1101 Programming Fundamentals
Step 1: Remove average.cpp from the project and add findErrors.cpp to the project. Here is
a copy of the source code.
01 // findErrors.cpp
02 // This program has one syntax error and one logic error. Find and fix them.
03 // PUT YOUR NAME HERE.
04 #include <iostream>
05 using namespace std
06
07 int main ()
08 {
09 double length = 0, // Length of a room in feet
10 width = 0, // Width of a room in feet
11 area; // Area of the room in sq. ft.
12
13 // Get the room dimensions
14 cout << "Enter room length (in feet): ";
15 cin >> length;
16
17 cout << "Enter room width (in feet): ";
18 cin >> length;
19
20 // Compute and display the area
21 area = length * width;
22 cout << "The area of the room is " << area << " square feet." << endl;
23
24 return 0;
25 }
Step 2: Put your name on line 3. Then compile the program. It contains one syntax error and one logic
error.
Step 3: Use the compiler error message to help you locate the syntax error and fix it.
Step 4: Once the program compiles with no errors, run the program, and examine the output. Analyze
what is going wrong so you can find and fix the logic error in the program. Once you have it running
correctly the output should look like the following:
Step 5: Follow the instructions your instructor gives you to print the final, correct findErrors.cpp
source code and the output the program displays when it runs.