0% found this document useful (0 votes)
29 views28 pages

ENGR112 - Lecture 3

The document discusses the structure and basic elements of a C++ program. It demonstrates how to write a simple program that displays text by including necessary headers, using namespace std, defining and calling the main function, and using cout to output text along with escape sequences for formatting.

Uploaded by

mmhygbwsvg
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)
29 views28 pages

ENGR112 - Lecture 3

The document discusses the structure and basic elements of a C++ program. It demonstrates how to write a simple program that displays text by including necessary headers, using namespace std, defining and calling the main function, and using cout to output text along with escape sequences for formatting.

Uploaded by

mmhygbwsvg
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/ 28

Structure of a Program

C++ Basics
Content: Today we will see
n Some basic rules about C++ coding
n The form of a C++ program
n How to display text messages in C++
n Some common errors in C++ coding
n Some good practices
My First Program
n Write a C++ program that displays the
following message on the screen

Welcome to ENGR 112 !


My First Program

Welcome to ENGR 112 !


My First Program
n Rule: any text
message must
be enclosed in
double
quotation
marks

“ Welcome to ENGR 112 ! ”


My First Program
n Rule: all C++
commands
must be in
small letters

cout “ Welcome to ENGR 112 ! ”


My First Program
n << is called
insertion
operator

cout << “ Welcome to ENGR 112 ! ”


My First Program
n Rule: every C++
statement
must end with
a semi-colon (;)

cout << “ Welcome to ENGR 112 ! ” ;


My First Program
#include <iostream> n iostream:
input/output
stream is a file
that includes
the
declarations of
the basic
cout << “ Welcome to ENGR 112 ! ” ;
standard input-
ouput library in
C++
n Note: no semi-
colon after this
line
My First Program
#include <iostream> n All the
using namespace std; elements of
the
standard C+
+ library
are
cout << “ Welcome to ENGR 112 ! ” ; declared
within the
namespace
std;
My First Program
#include <iostream> n main() is a
using namespace std; function where
all C++
programs start
int main() their execution
{ (i.e. The entry
point)
cout << “ Welcome to ENGR 112 ! ” ;
n main() must
appear exactly
once in every
C++ program
} n No semi-colon
(;) after main()
n return 0; tells
the OS that the

My First Program program has


worked as
expected
without errors
#include <iostream> during its
using namespace std; execution
n { and }
int main() indicates the
begin and the
{ end of the
cout << “ Welcome to ENGR 112 ! ” ; function
respectively.
(function will
be seen later)
return 0; n main is a
} particular
function
My First Program
#include <iostream> n The output of
using namespace std; the program

int main()
{
cout << “ Welcome to ENGR 112 ! ” ;

return 0;
}

Welcome to ENGR 112!


General form of a C++ program
1. // Program description (comments)
2. #include directives
3. using namespace std;
Numbers 4. int main()
Are not
Part 5. {
Of the
program 6. variable declarations
7. executable statements
8. return 0; //ending the program
9. }
C++ comments
n Comments are explanatory notes
n Increase the program readability
n They are ignored by the compiler

n There are two ways to include comments in a


program:
// single line comment: starts with
// double forward slash signs to the end of
// that line.

/* Block Comment: A slash followed by an


asterisk marks the start of a multiple line
comment. It ends with an asterisk followed by
a slash. */
1 // Fig. 1.2: fig01_02.cpp Single-line comments. Ignored
by Compiler
2 // A first program in C++.
Preprocessor directive which tells the
3 #include <iostream> pre-processor to include input/output
stream header file <iostream>.
4 using namespace std;
Name cout belongs to namespace
5 /* function main begins program execution */ std.
Function main appears exactly
6 int main() once in every C++ program..
7 { Function main returns an integer value. Statement is a simple or compound expression
for computer to perform, end with a
8 cout << “Welcome to ENGR 112!\n”; semicolon(;)
9 Stream insertion operator. cout is a standard output stream
Left brace { begins function body.
10 return 0; // indicate that program ended successfully
11 Corresponding right brace } ends function body.
Keyword return is one of several means to exit function;
12 } // end function main
value 0 indicates program terminated successfully.

Welcome to ENGR 112!

n using namespace std; Otherwise you have to use std::cout


n C++ is case sensitive. Cout is different than cout.
<iostream> file
• What is the output of this program?

1 // Fig. 1.4: fig01_04.cpp


2 // Printing a line with multiple statements.
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 int main()
7 {
Unless new line '\n' is explicitly
8 cout << “Welcome "; specified, the text continues on
the same line.
9 cout << “to ENGR 112!";
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main
Welcome to ENGR 112!
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 int main()
7 {
8 cout << “Welcome to ENGR 112";
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main

Modify the above code to display the following output:


Welcome
to

ENGR 112
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
Using newline characters to print
6 int main() on multiple lines.
7 {
8 cout << “Welcome\nto\n\nENGR 112\n";
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main

Welcome
to

ENGR 112
Escape Sequences
n Rule: all escape sequences must be within the quotations
Escape Description Example Output
Sequen
\n Newline. Position the screen cursor cout<<“Hello \n World”; Hello
to the beginning of the next line. World
\t Horizontal tab. Move the screen cout<<“Hello \t World”; Hello World
cursor to the next tap stop.
\r Carriage return. Position the cout<<“Hello \r Word”; Word
screen cursor to the beginning of
the current line.
\a Alert. Sound the system bell cout<<“Hello \a”; Beep
\\ Backslash. Used to print backslash cout<<“hello \\ World”; Hello \ World
character
\” Double quote. Used to print double cout<<“\”hello\” ”; “hello”
quote character
21
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 int main()
7 {
8 cout << "Welcome”<<endl<<“to\n”;
9 cout << endl<<“ENGR 112”<<endl;

10 return 0; // indicate that program ended successfully

11
12 } // end function main
Welcome
to

ENGR 112
Program Errors
1 /* Please find out the ERRORS,
2 Enjoy………………………*/
3 #include (isotream);
4 using name space std
5 // function main begins program execution
6 int main{ }
7 {
8 Cout >> “C++ “,
9 cout >> “is fun! “,
10 cout >> “endl”;
11
12 return ; // indicate that program ended successfully
13 } // end function main
Program Errors
1 /* Program without Errors,
2 Enjoy………………………*/
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 int main()
7 {
8 cout << “ C++ “;
9 cout << “is fun! “;
10 cout << endl;
11
12 return 0; // indicate that program ended successfully
13 } // end function main

C++ is fun!
What makes a bad program?
n Writing Code without detailed analysis and
design

n Repeating trial and error without


understanding the problem

n Writing tricky and dirty programs


Exercise
n Write a C++ code that displays your
name, surname as shown below:

Mohamed
Abdul-Aziz
Exercise: solution
n Analysis:
n Output : text showing Name and
Surname in separate lines
n Input: None
n Process: Display the text mentioned
above
Exercise: solution (cont)
1 /* Program displaying name and surname */
2 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 int main()
7 {
8 cout << “Mohamed“;
10 cout << endl ;
11 cout << “Abdul-Aziz“;

12 return 0; // indicate that program ended successfully


13 } // end function main

You might also like