0% found this document useful (0 votes)
23 views17 pages

Lec-03 - W3 (Layout of C++ Program)

This document provides an introduction to C++ programming, covering the layout of a C++ program, sample programs, and basic operations such as adding two integers. It also discusses testing and debugging, including types of errors such as syntax, run-time, and logic errors. The document includes examples of C++ syntax and the use of input/output streams.

Uploaded by

aurangzeb5060
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)
23 views17 pages

Lec-03 - W3 (Layout of C++ Program)

This document provides an introduction to C++ programming, covering the layout of a C++ program, sample programs, and basic operations such as adding two integers. It also discusses testing and debugging, including types of errors such as syntax, run-time, and logic errors. The document includes examples of C++ syntax and the use of input/output streams.

Uploaded by

aurangzeb5060
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/ 17

Introduction to C++

Programming
Lecture – w3
Outline

• Layout of a C++ Program


• Sample C++ Program
• Another Simple Program: Adding Two Integers
• Testing and Debugging
Layout of a C++ Program

#include <iostream>
using namespace std;
Program starts here
int main()
{
Variable_Declarations

Statement_1
Statement_2

Statement_Last

return 0;
} Program ends here
Layout of a C++ Program

#include <iostream> include directive


using namespace std;
standard namespace
int main()
{ main function
Variable_Declarations

Statement_1
Statement_2 executable statements

Statement_Last

return 0; return statement


}
Sample C++ Program
1 // Program_01.cpp
Comments
2 // A first program in C++ Written between /* and */ or following a
3 #include <iostream> //
4 Improve program readability and do not
5 int main() cause the computer to perform any action
6 { preprocessor directive
7 std::cout << "Welcome to C++!\n";
Message to the C++ preprocessor
8 Lines beginning with # are preprocessor directives
C++ programs contain<iostream>
#include one or more functions, exactly one to
tells the preprocessor
9 return 0; // indicate that program ended
of which must be main
successfully
10 }
include the contents of the file <iostream>, which
Parenthesis used to indicate a function
includes input/output operations (such as printing to
int means that main "returns" an integer value.
the screen).
Welcome to C++!
Left brace { begins the body of every
return is one a way to exit a function. function and a right brace } ends it
return 0, in this case, means that the
Prints the string of characters contained between the
program terminated normally.
quotation marks.

The entire line, including std::cout, the <<


operator, the string "Welcome to C++!\n" and
the semicolon (;), is called a statement.

All statements must end with a semicolon.


Sample C++ Program
➢std::cout
✓standard output stream object
✓“connected” to the screen
✓we need the std:: to specify what "namespace" cout belongs
to
• we shall remove this prefix with using statements

➢<<
✓stream insertion operator
✓value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
std::cout << " Welcome to C++!\n"

➢\
✓escape character
✓indicates that a “special” character is to be output
Sample C++ Program
Escape Description
Sequence
\n Newline. Position the screen cursor to the beginning of the
next line.

\t Horizontal tab. Move the screen cursor to the next tab stop.

\r Carriage return. Position the screen cursor to the beginning


of the current line; do not advance to the next line.

\a Alert. Sound the system bell.

\\ Backslash. Used to print a backslash character.

\" Double quote. Used to print a double quote character.

• There are multiple ways to print text


• Following are more examples
Sample C++ Program

1. #include <iostream>
2. int main()
3. {
4. std::cout << "Welcome ";
5. std::cout << "to C++!\n";
6. return 0; // indicate that program ended successfully
7. }

Welcome to C++!

Unless new line '\n' is specified, the text continues


on the same line.
Sample C++ Program

1. #include <iostream>
2. int main()
3. {
4. 7 std::cout << "Welcome\nto\n\nC++!\n";
5. return 0; // indicate that program ended
successfully
6. }

Welcome
to

C++!
Sample C++ Program
1 // Program_01.cpp

2 // first program in C++

3 #include <iostream>

5 int main()

6 {

7 std::cout << "Welcome to C++!\n";

9 return 0; // indicate that program ended successfully

10 }
Another Simple Program: Adding Two
Integers
➢>> (stream extraction operator)
✓When used with std::cin, waits for user to input a value and
stores the value in the variable to the right of the operator.
✓user types number, then presses the Enter (Return) key to send
the data to the computer
✓Example:
int myVariable;
std::cin >> myVariable;
waits for user input, then stores input in myVariable

➢= (assignment operator )
• assigns value to a variable
• binary operator (has two operands)
sum = variable1 + variable2;
Addition operator
Another Simple Program: Adding Two
1 // Fig. 1.6: fig01_06.cpp
2 // Addition program
Integers
3 #include <iostream>
4
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 std::cout << "Enter first integer\n"; // prompt
Notice how std::cin is used to get
10 std::cin >> integer1; // read an integer
user input.
11 std::cout << "Enter second integer\n"; // prompt
12 std::cin >> integer2; // read an integer
13 sum = integer1 + integer2; // assignment of sum
14 std::cout << "Sum is " << sum << std::endl; // print sum
15
16 return 0; std::endl flushes the buffer
// indicate that program ended successfully
17 } and prints a newline.

Enter first integer


45
Enter second integer
72
Sum is 117 Variables can be output using std::cout <<
variableName
Another Simple Program: Adding Two Integers

#include <iostream>
using namespace std;

int main()
{
int number1, number2, sum;

cout << "Enter first number: ";


cin >> number1;

cout << "Enter second number: ";


cin >> number2;

sum = number1 + number2;

cout << "Sum = " << sum << “\n”;

return 0;
}
Testing and Debugging

➢Bug
A mistake/error in the program
➢Debugging
The process of eliminating bugs in a program
Testing and Debugging

• Types of program errors:


• Syntax errors
• Violations of the rules of the programming language
• Run-time errors
• Detected by computers when the program is run (numeric
calculations)
• Logic errors
• Mistakes in the underlying algorithm or translating the
algorithm into C++ language
Sample C++ Program

Try this: #include <iostream>


using namespace std;
Write a program that int main()
displays the product {
of two integers int number1, number2, product;

cout << "Enter first number: ";


cin >> number1;

cout << "Enter second number: ";


cin >> number2;

product =…………………..?

cout << “Product = " << product << “\n”;

return 0;
}
Thank You

You might also like