Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Faculty of Engineering

NAME: GABRIEL WONG JUNG YIT


STUDENT ID: 1001747052

Subject : EE209 Computing for Engineers

Lab 1: Introduction to use C++ compiler and the Execution of C++


Programs
Objectives:

1. Invoke the editor, create a simple C++ program file and save that file.
2. Retrieve the file mentioned above and modify it.
3. Enter a C++ program and execute it.
Equipment/Apparatus:

 Computer with windows operating system


 C++ compiler and editor
Learning Outcomes:
At the end of the session, the students should be able to:
1. Use basic elements of C++ to write a C++ program.
2. Debug errors if any and execute the C++ program.
Experiment 1.1:
This experiment introduces the compiling process and the manner in which your
compiler reports the errors that it finds. Error messages vary greatly from one system to
another. Some systems are very helpful to the programmer, and some are not. Most C++
compilers pinpoint the line where the error has occurred and display a short error message.
It is then up to you to play the part of detective, going back into the source program
and searching for the error yourself. Often one small error can spawn several misleading
messages pointing to false errors.
A common procedure when trying to find and remove errors from a program (a process
called "debugging") is to try to compile the program each time an error has been corrected
rather than insisting on resolving all the error messages at once. It often happens that one
correction will cause other misleading error messages to dissipate.

Step 1. The following document is a simple program in the C++ programming language.
Using the editor in your particular software development environment, type the
program as it appears here and save it for future reference. Be careful to include all of
the punctuation marks and the braces.
// This is our first C++ program.
#include <iostream>
int
main()
{
cout << "What is your favorite\n";
cout << "flavor of ice cream?\n";
return
0; }
Step 2. Retrieve the program from Step 1 and compile it. If the compiler finds errors
(which would be typing errors), correct them and try again. Execute the final, correct
program. What happens?

“using namespace std;” is missing after “#include <iostream>”.


The correct program.

Step 3. In case you didn't have any typing errors on your first attempt, we'll introduce
some now. Change the word cout in the source program to couts and try to compile the
altered version. How does your compiler inform you of this error?

Compiler inform that Error: ‘couts’ was not declared in this scope.
Step 4. Correct the error introduced in Step 3, and then remove the semicolon from the
end of the line
cout << "What is your favorite\n";
Try to compile this altered version. How does your compiler respond?

Compiler respond that Error : expected ‘;’ before ‘cout’


Step 5. Correct the error introduced in Step 4, and then remove the closing brace } at
the end of the program. Try to compile this altered version. How does your compiler
respond?

Compiler respond that error: expected ‘}’ at end of input

A Simple C++ Program


Let us examine the program listed below, which is the program used in Experiment
1.1
// This is our first C++ program.
#include <iostream>
int
main() {
cout << "What is your favorite\n";
cout << "flavor of ice cream?\n";
return
0; }

In addition to statements that will ultimately be translated into machine language, a C+


+ program can contain explanatory remarks for the aid of human readers (like your
professor). These remarks, known as comments, are placed after the characters / /. In
turn, the C++ compiler ignores everything appearing on the same line after these special
characters. In our example program, the first line
// This is our first C++ program.

is a comment. Comments have a variety of uses. They can be inserted to clarify a section of
a program that might otherwise be hard to understand. They can also be used to give
information about the creation of a program such as the date created and by whom.
The statement
#include <iostream>
in our example program is an example of a preprocessing directive. These directives cause
the source program to be modified before the compiling process begins and are
signified by beginning with a # symbol. The directive in our example causes a copy of
the standard header file named iostream to be inserted at the beginning of the source code
when compilation occurs. This file contains information that the compiler will need to
perform its task. In particular, our example refers to the special object cout (which we will
explain shortly) through which information can be sent to the monitor screen. Our
program does not contain the details of cout but merely communicates with the object
by means of the operator <<. The file ios tream contains the information needed by the
C++ compiler to create the required link between our program and the system library.
Names of standard header files are enclosed by angle brackets, < and >, as in our
example. Names of nonstandard header files, such as those you may write yourself, are
enclosed in quotation marks, as in
#include "HomeMade . h"
A C++ program consists of units called functions. (We will learn about functions
shortly.) Every program contains at least one function called main. Execution of a C++
program always begins in the function main. That is, the function main represents the
beginning of the program even though the function may appear much later in the written
program. The line
int main()
in our example indicates the beginning of the function main. This opening line of a
function is known as a function header. We'll learn more about function headers in
later laboratory sessions. For now we note that such a header consists of three parts: a
return type (in our example int), the name of the function (in our example main), and a
parameter list (in our example an empty parenthetical expression). The fact that the
function header in our example has a return type of int indicates that the operating system
should expect to receive a numeric (integer) value when our program terminates. This value is
used to report whether our program executed successfully. We'll return to this point shortly.
The actual "meat" of a function is placed between braces. Immediately following the
opening brace is the declarative part of the function. It is here the terminology relating to that
particular function is introduced. Our example program is so simple that it does not contain
a declarative part. Instead, the function main in our example consists of only a procedural
part—the part containing the instructions to be followed when the function is executed.
The procedural part of a function always follows the declarative part.
Statements in a C++ program are terminated by semicolons. Although not mandatory, it is
customary to place each statement on a separate line and to use indentation to help the
reader identify related portions of the program.
Each statement in the procedural part of our example program uses the predefined
object cout and the operator <<. The object cout is just one of many standard units that
are so commonly used in programs that C++ provides them for your use in standard C++
libraries. You will learn more about cout in later laboratory sessions. For now we need
merely note that the statement.

cout << "What is your favorite\n";


causes the characters that are enclosed in quotation marks to be printed on the monitor
screen. Thus, when executed, our example program will cause the two lines
What is your favorite
flavor of ice cream?
to appear on the screen.
The last line in the function main
return 0;
indicates that our program has finished its task and that control should be returned to the
operating. As indicated in the function's header, the operating system will be expecting
to receive a numeric value indicating whether our program executed successfully. This is
the purpose of the value 0 in the return statement. In general, returning the value 0 means
that the program executed successfully; other values are used to indicate various errors.
Experiment 1.2
Step 1. Omit the following
line #include
<iostream>
from the program in Experiment 1.1 and try to compile the modified version. Record
what happens.
Step 2. Remove both cout statements from the modified program in Step 1 and try to
compile the program. Explain the results.

The program expects primary-expression before ‘<<’ token. The “c” in cout refers to
“character” and “out” means output. Cout means character output and cout is used with
the “<<” in order to display a stream of characters.
Experiment 1.3
Step1. Insert the following lines into the procedural part of the function main in Experiment
1.1. Explain how the output produced by each of these statements differs from the
others.
cout << "Chocolate, butterscotch, strawberry, vanilla?\n";
cout << "Chocolate, butterscotch,\nstrawberry, vanilla?";
cout << "Chocolate, butterscotch\n\nstrawberry, vanilla?"

The first line statement is ending with “\n” at the end of the statement and
results a newline is introduced after the statement. While the “\n” is inserted in
the middle of the second statement and results the statement behind “\n” is in
the newline. The third statement is connecting with the back of second
statement because there is no new line at second statement. There is two “\n” in
the middle of the third statement and it results two blank lines in between.

Step 2. What does the \n character combination mean?


“\n” means move to a new line.

Experiment 1.4
Insert the following lines into the procedural part of the function main in Experiment 1.1. What
rule can you derive about the placement of comments?
cout << "Send money quick!\n"; // To Mom!
cout << "Send money quick!\n" // To Mom! );
cout << "Send // To Mom! money quick!\n");

“//” is a forward slash, which is a function to write comments to the statements. Anything can
be written in the comments, but C++ program does not run them. The first statement with the
semicolon is placed before the forward slash which is correct. The semicolon is after the
comment and causing the program cannot recognize it. Hence, the semicolon needs to be
placed in front of the comment. For the third statement, the forward slash is placed inside the
double quote, which means it is not a comment rather a statement to be displayed on the
screen.

Experiment 1.5
Step 1. Insert the following lines into the procedural part of the function main in Experiment
1.1. What rule can you derive about printing sentences containing quotation marks?
cout << "Oh, I love to program in C++. . . \n";
cout << "Oh, I love to "program" in C++. . . \n";
cout << "Oh, I love to ""program"" in C++. . . \n";
cout << "Oh, I love to \"program\" in C++. . . \n";
The compiler shows the error expected ‘;’ before ‘program’

```````````````````````````````````````````````````````````
`````````````````````````````````````````````;
This compiler shows the double quotation because it pair with backslash.

Step 2. What is the meaning of the backslash mark?

Backslash mark is used as a delimiter to begin a two-character escape sequence. When a backslash is
placed in front of a group of characters, it tells the compiler to escape from the way these characters
are normally interpreted. The combination of a backslash and these characters is called an escape
sequence.
Post-Laboratory Problems

1.1. Design an algorithm and write a program that prints the message
My name is xyz,
I like EE209 Computing for Engineers.

(Note type your name instead of xyz)


a. all one line

b. on two line

c. inside a box draw with asterisks


1.2. Find the errors in the following program.
# include iore.h
mian (}
(
couts << \n"I like to write/n before I've read it.\n\n;
cout << "Then, with my pen, I always edit."\n;
Cout >> "But, with computer\s, now I type;
cout <<("An never, ever get it right./n")
}

Errors found in codes:


1) #include iore.h should be #include <iostream>.
2) Missing ‘using namespace std;’.
3) main(} should be main ().
4) ‘(’ should be ‘{’.
5) couts should be cout.
6) \n should be inside the double quotation.
7) Cout should be cout and >> should be <<.
8) n should be \n.
9) There must be an end quotation mark after a statement.
10) \n should be included inside the double quotation.
11) Wrong operator for cout, should be << instead of >>.
12) Typing error of \s, should be \n.
13) Missing end quotation after now I type.
14) Brackets should be included inside the quotation.
15) Missing ‘return 0;’ after finishing programming.

The corrected program.


1.3. Design an algorithm and Write a program that converts degree Fahrenheit (TF) to degrees

(
Celsius (TC) Recall that TC =( TF−32 )∗ 9( ))
5

 Choose the suitable variables type.


 The program should ask the user to enter the degree Celsius (TC).
 Your program should also output your name and student ID number at the top.
Post-Laboratory Problems

1.1. Design an algorithm and write a program that prints the message
My name is xyz,
I like EE209 Computing for Engineers.

(Note type your name instead of xyz)


a.all on one line
b. on two lines
c. inside a box drawn with asterisks

1.2. Find the errors in the following


program.
# include iostream
mian (}
(
couts << \n"I like to write/n before I've read it.\n\n;
cout << "Then, with my pen, I always edit."\n;
Cout >> "But, with computer\s, now I type;
cout <<("An never, ever get it right./n")
}

1.3. Design an algorithm and Write a program that converts degree Fahrenheit (TF) to

(
degrees Celsius (TC) Recall that TC=( TF−32 )∗ 9
5
( ))
 Choose the suitable variables type.
 The program should ask the user to enter the degree Celsius (TC).
 Your program should also output your name and student ID number at
the top.

Sample Output:

You might also like