0% found this document useful (0 votes)
13 views

Lab 01

The document describes a lab exercise on creating a "Hello World" program in C++. It discusses compiling and running a sample greeting program that prompts the user for their name and prints a greeting. It then covers the three types of errors that can occur in programs: syntax errors, run-time errors, and logic errors. Examples of each are demonstrated through sample code and exercises to identify and fix errors in additional code samples.

Uploaded by

Vishvan Varma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab 01

The document describes a lab exercise on creating a "Hello World" program in C++. It discusses compiling and running a sample greeting program that prompts the user for their name and prints a greeting. It then covers the three types of errors that can occur in programs: syntax errors, run-time errors, and logic errors. Examples of each are demonstrated through sample code and exercises to identify and fix errors in additional code samples.

Uploaded by

Vishvan Varma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TCP1101 Programming Fundamentals

Lab01 – C++ “Hello World”


To begin
• Follow the instructions your instructor gives you to log on to your system and create a folder named
Lab1 in your work space.
• Copy all the files if there is any to your Lab1 folder.
• Follow the instructions your instructor gives you to run the IDE your class will use for your C++
programs.

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:

greeting.o – 0 error(s), 0 warning(s)

Notice what the message looks like on your system.

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. ______________________________________________________

Computer Program Errors

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.

LAB 1.2 – Syntax Errors

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:

greeting.cpp:13: error: expected ‘;’ before “cin”

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

LAB 1.3 – Run-time Errors

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:

Enter two numbers separated by one or more spaces: 10 5


The average of the two numbers is: #INF

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:

Enter two numbers separated by one or more spaces: 10 5


The average of the two numbers is: 12.5

LAB 1.4 – Logic Errors


A logic error causes a program to work incorrectly. It doesn’t break any syntax rules and doesn’t tell
the computer to do anything it is unable to do. Instead it occurs when the program logic is wrong
because what the programmer tells the program to do does not match what he or she means for it to do.
If you were explaining to someone how to do laundry, it would be a logic error to tell them to put the
laundry in the oven, when what you meant was for them to put it in the washer. Another logic error
would occur if you told them to wash the laundry and then fold it, but forgot to tell them to dry it. It
would also be a logic error if you got the steps out of order and told them to first fold the laundry, then
dry it, and then finally to wash it. Likewise, for a program to be correct each instruction must be correct,
no instructions must be omitted, and the instructions must be carried out in the right order.
Let’s look at a logic error that causes a program to carry out two mathematical operations in the wrong
order.
Step 1: Look again at the output created by the average.cpp program in Step 5 of Lab 1.3 above.
Notice that the user entered 10 and 5, but the program reported the average to be 12.5, rather than
7.5. The error occurs on line 19. To find an average of a set of values, you must add the values before
you divide by the number of values. But the code on line 19 tells the computer to divide the value stored
in num2 by size before adding the result to the value stored in num1. You will learn more in chapters
2 and 3 about how to write correct mathematical statements in C++. For now, just add parentheses on
line 19 so it says:
average = (num1 + num2) / size;

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.

Enter two numbers separated by one or more spaces: 10 5


The average of the two numbers is: 7.5

4
TCP1101 Programming Fundamentals

LAB 1.5 – Fix the Errors

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:

Enter room length (in feet): 15


Enter room width (in feet): 10
The area of the room is 150 square feet.

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.

You might also like