0% found this document useful (0 votes)
18 views30 pages

Chapter 4

Uploaded by

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

Chapter 4

Uploaded by

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

‫ِم‬‫ْي‬ ‫ِح‬ ‫َّر‬‫ال‬ ‫ِن‬ ‫ٰم‬ ‫ْح‬ ‫َّر‬‫ال‬ ‫ِه‬ ‫الل‬ ‫ِبْس ِم‬

CHAPTER 4
INPUT & OUTPUT

PRESENTED BY
ZILL-E-SUBHAN (45)
HASAN BILAL (25)
PRESENTATION OUTLINE

 INPUT & OUTPUT


 STANDARD OUTPUT
 ESCAPE SEQUENCES
 C++ MANIPULATORS
 endl MANIPULATOR
 setw MANIPULATOR
 setprecision MANIPULATOR
 fixed MANIPULATOR

 STANDARD INPUT
INPUT & OUTPUT

 The process of giving a command or set of


instructions to computer is INPUT , It is usually given
by keyboard.
 The process of getting required data or information
from computer is OUTPUT , It is usually displayed on
monitor screen.
 C++ uses streams to perform input & output
operations.
 IOSTREAM is used for declaration of all input & output
stream objects , It should must be included in
STANDARD OUTPUT

 STANDARD OUTPUT refers to the output displayed on

monitor.

 C++ uses cout stream to display standard output ,

stands for

console output.

 cout uses insertion operator (<<) , and the message


SYNTAX

 SYNTAX OF USING cout OBJECT IS AS FOLLOWS :-


 cout<<Variable/Constant/Expression ;

 cout name of object used to display standard


output.

 << Insertion operator , It sends output to cout


object
which sends output to screen. Right side of
operator
must be an expression or manipulator.

 Variable/Constant/Expression It is the variable ,


constant , expression
whose value is displayed on screen.
EXAMPLE

 cout << “Pakistan Zindabad”;


 The above line will display Pakistan Zindabad on
screen.

 cout << 720;


 The above line will display 720 on screen.

 cout << a;
 The above line will display value of variable on
screen.

 cout << “The Value of N is : ”<<N;


 The above line will display The Value of N is on
screen along with value of variable N.
ESCAPE SEQUENCES

 These are special characters used to perform various

tasks .

 They are not displayed in the output.

 They are used in combination with backlash ‘\’ ,

called escape character.

 Diff erent escape sequences are used in C language.


ESCAPE SEQUENCES & THEIR
PURPOSES
This character will pay beep

sound during execution. For

example :-

cout<<“Hello \a World ” ; will \a


display

Hello World

After displaying ‘ Hello

‘,computer will play beep and


This character is used to insert

backspace in output. For example :-

\b
cout<<“Hello \b World ” ; will display

Hell World

After displaying ‘ Hello ‘ , \b will

delete ‘o’ and then print ‘World’. So

‘Hell World’ will be displayed.


This character is used to insert new

line in the output. For example :-

cout<<“Hello \n World ” ; will display

Hello

 World

After displaying ‘ Hello ‘ , \n will


\n
move the cursor to next line and then

print ‘World’. So ‘Hello World’ will be

displayed as :-

Hello

World
This character is used to move
the cursor to the beginning of
line. For example :-
cout<<“Hello \r World ” ; will
display
World
\r
After displaying ‘ Hello ‘ , \r
will move cursor to beginning
of line and then print ‘World’
that overwrites ‘Hello’. So
‘World’ will be displayed.
This character is used to insert a TAB

in output. For example :-

\t
cout<<“Hello \t World ” ; will display

Hello World

After displaying ‘ Hello ‘ , \t will

inserts a TAB and then print ‘World’.

So ‘Hell World’ will be displayed.


PROGRAMS FOR ESCAPE
SEQUENCES
#include <iostream>
using namespace std;
int main ()
{
 int a , b ;
 cout<<"Enter values of a & b : \n";
 cin>>a>>b;
 cout<<"The Value of a is "<<a<<"\
n"<<"&"<<"\n"<<"The Value of b is
"<<b<<endl;
}
PROGRAMS FOR ESCAPE
SEQUENCES
#include <iostream>
using namespace std;
int main ()
{

 cout<<"C++ \r Programming";
}
PROGRAMS FOR ESCAPE
SEQUENCES
#include <iostream>
using namespace std;
int main ()
{

 cout<<"INTRO TO \t COMPUTING";
}
C++ MANIPULATORS

 C++ manipulators are special functions that control


formatting output operations.
 They are used to adjust the appearance of data.

 Common examples includes :-

 endl

 setw

 fi xed

 setprecision
The word ‘endl’ stands for
end of line. The endl
manipulator is used to
start new line. It works
similar to ‘\n’ escape
sequence character.
EXAMPLE :- en
 cout<<“Hello”<<end<<
“World”; dl
 Will print :-
 Hello
 World
 The word ‘setw’ stands for set width. The
setw manipulator is used to display value of
an expression in specifi ed columns.
 Value of expression can be string or number.

 If the value is less than specifi ed columns ,


additional columns will left blank from left
side.
 The output automatically uses required
set
columns if output is larger than specifi ed
columns.
w
 It is only applied to the values inserted
after it.
 The setw manipulator is part of iomanip.h
library fi le.
SYNTAX

Syntax of setw is as follows :-

setw(n)

The ‘n’ indicates number of specifi ed

columns for value to be displayed.


EXAMPLE

Suppose that value of a is 2 & of b is 47


, then:-

cout<<setw(5)<<a<<setw(5)<<b<<se
tw(15)<<“Programming” will display :-

 2 47 Programming
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
 int n = 3928; Progra
 double d = 91.5; m for
 char str[] = "OOP using C++"; setw

cout<<"("<<setw(5)<<n<<")"<<end
l;

cout<<"("<<setw(10)<<d<<")"<<en
dl;

cout<<"("<<setw(20)<<str<<")"<<e
ndl;

 It is used to set number of digits to be

displayed when outputting fl oating-

point numbers i.e 3.57 , 84.492 .

 It controls how many number should be


setprecisio
n
displayed.

 EXAMPLE :-

 int x = 36.48290;

 cout<<setprecision(4)<<36.48290;

will display :-

 36.48
 It is used to set number of digits to be

displayed when outputting fl oating-point

numbers i.e 3.57 , 84.492 .

 It controls how many number should be

fixe
displayed after decimal point.

 EXAMPLE :-

 int x = 36.48290; d

cout<<fi xed<<setprecision(4)<<36.48290;

will display :-

 4829
 #include <iostream>
 #include <iomanip>
 using namespace std;
 int main ()
{
 double r , n1 = 1320.36473 , n2 =
258.583;
 r = n1/n2;
Program
 cout<<r<<endl; for fi xed &

setprecisio
cout<<fi xed<<setprecision(5)<<r<<endl; n

cout<<fi xed<<setprecision(4)<<r<<endl;

cout<<fi xed<<setprecision(3)<<r<<endl;

cout<<fi xed<<setprecision(2)<<r<<endl;

cout<<fi xed<<setprecision(1)<<r<<endl;
}
STANDARD INPUT

It is referred as input via keyboard.


C++ uses cin stream to get standard
input.
It stands for console input , which uses
extraction operator (>>) .
The operator must be followed by a
variable , which is used to store data.
SYNTAX

 SYNTAX OF USING cin OBJECT IS AS FOLLOWS :-

 cin<<var ;

 cin name of object used to get standard input.

 >> Extraction operator , It gets input from cin


object. It then stores the input to variable. Its
left side must be of stream like cin. Its right side
must be of simple data type. .
 var It is the variable in which output is stored.
EXAMPLE

cin>>x;
 The above line will get value from
keyboard & store it in variable x.

cin>>a>>b>>c;
 The above line will get three value
from keyboard & store it in variables a , b ,
c .
ANY JAZAK
QUESTIONS ? ALLAH

You might also like