Chapter 4
Chapter 4
CHAPTER 4
INPUT & OUTPUT
PRESENTED BY
ZILL-E-SUBHAN (45)
HASAN BILAL (25)
PRESENTATION OUTLINE
STANDARD INPUT
INPUT & OUTPUT
monitor.
stands for
console output.
cout << a;
The above line will display value of variable on
screen.
tasks .
example :-
Hello World
\b
cout<<“Hello \b World ” ; will display
Hell World
Hello
World
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
\t
cout<<“Hello \t World ” ; will display
Hello World
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.
setw(n)
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
EXAMPLE :-
int x = 36.48290;
cout<<setprecision(4)<<36.48290;
will display :-
36.48
It is used to set number of digits to 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
cin<<var ;
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