Transcript Lecture 5

Lecture #5:
iostream functions
‫دوال اإلدخال واإلخراج‬
Dr. Hmood Al-Dossari
King Saud University
Department of Computer Science
4 March 2012
Overview

Input Functions )‫(دوال اإلدخال‬






cin.get();
cin.ignore();
cin.putback();
cin.peek();
cin.clear();
Output Functions (‫)دوال اإلخراج‬






setprecision();
fixed
showpoint
setfill
setw();
left/right
Input Functions: cin.get()
•
•
•
•
•
‘>>’ will skip all leading whitespace characters.
Consider the variable declarations:
char ch1, ch2;
int num;
Suppose, the input is: A 25
Consider the following statement:
cin>>ch1>>ch2>>num;
Output:
I. ‘A’ is stored in ch1,
II. ‘2’ is stored in ch2 and
III. 5 is stored in num.
Note: the blank is skipped by the extraction operator ‘>>’,.
Input Functions: cin.get()
•
•
•
•
Using get function with cin will take spaces into account.
The syntax of cin statement with get function:
cin.get(varChar);
In previous example:
cin.get(ch1);
cin.get(ch2);
cin>>num;
Output:
I. ‘A’ is stored in ch1,
II. a blank is stored in ch2 and
III. 25 is stored in num.
Input Functions: cin.ignore()
•
•
Ignore function is used to discard a portion of the input.
The syntax of cin statement with ignore function:
cin.ignore(intExp, chExp);
•
Example:
char first, last;;
first=cin.get();
cin.ignore(256,' ');
last=cin.get();
cout<< "Your initials are :" << first << last<<"\n";
•
•
Suppose, the input is: Hmood Al-Dossari
Output:
Your initials are :HA
Input Functions: istream.putback()
• putback function puts the character ch back to the input
stream. Thus, the next extracted character will be ch.
•
The syntax of cin statement with putback function:
istream.putback(varChar);
istream.get(varChar);
Where, varChar is an input stream variable.
Input Functions: istream.peek()
• peek function reads and returns the next character without
extracting it.
•
The syntax of cin statement with peek function:
variable = istream.peek();
Input Functions: putback and peek
Input Functions: cin.clear()
•
When an input stream enters the fail state, the system ignores all
further I/O stream. Thus, programmer can use the stream function
clear to restore the input to a working state.
•
The syntax of cin statement with peek function:
cin.clear();
Example:
int a = 23;
int b = 34;
cin>>a>>b;
cin.clear();
cout<<a<<“ , "<<b;
Suppose the input is: 12 R
Output: 12 , 34
Suppose the input is: 19 56
Output: 19 , 56
Activity 1
•
-1- ‫نشاط‬
‫أكتب مخرجات البرنامج التالي‬
int a = 23;
int b = 34;
cin>>a>>b;
cin.clear();
cout<<a<<“ , "<<b;
Suppose the input is: R 12
Output:
Output Functions: setprecision()
•
•
setprecision function is used to control the output of floating-point
numbers.
a maximum of six decimal places for default output of floatingpoint.
•
The syntax of setprecision:
cout<<setprecision(n);
•
This function requires #include<iomanip.h> header file.
Example:
float a = 2.3889;
cout<<Setprecision(3);
cout<<a;
Output:
2.38
Output Functions: fixed
•
fixed sets the output of floating-point numbers in a fixed decimal
format on the standard device.
•
The syntax of fixed:
cout<<fixed;
•
Note: to disable the manipulator fixed function to default setting,
you can use:
cout.unsetf(ios::fixed);
Example:
float a = 2.3889;
cout<<fixed;
cout<<a;
Output:
2.38890
Output Functions: showpoint
•
•
•
If the decimal part of a decimal number is zero, the output may not
show the decimal point and the decimal part.
showpoint function forces the output to show the decimal point
and decimal part.
The syntax of showpoint:
cout<<showpoint;
Example:
float a = 2;
cout<<fixed;
cout<<showpoint;
cout<<a;
Output:
2.00000
Output Functions: setw()
•
setw is used to output the value of an expression in specific
columns.
The value of the expression can be either a string or a number.
•
•
The syntax of setw:
cout<<setw(n)<<“............”;
Example:
cout<<setw(12)<<“Hmood”;
1
2
3
4
5
6
7
8
9
10
11
12
H
m
o
o
d
Output Functions: setfill()
•
setfill is used to allow us to fill the unused columns with a character
other than a space.
The syntax of setfill:
cout<<setfill(‘&’);
•
Example:
cout<<setfill’&’;
1
2
3
4
5
6
7
8
9
10
11
12
&
&
&
&
&
&
&
H
m
o
o
d
Output Functions: left function
•
left is used for left-justified.
•
The syntax of left:
cout<<left;
We can disable left function by using the stream function unsetf.
ostream.unsetf(ios::left);
•
Example:
cout<<left;
cout<<setw(12)<<“Hmood”;
1
2
3
4
5
H
m
o
o
d
6
7
8
9
10
11
12
Output Functions: right function
•
right is used for right-justified.
•
The syntax of right:
cout<<right;
We can disable right function by using the stream function unsetf.
ostream.unsetf(ios::right);
•
Example:
cout<<right;
cout<<setw(12)<<“Hmood”;
1
2
3
4
5
6
7
8
9
10
11
12
H
m
o
o
d
Activity 2
•
-2- ‫نشاط‬
‫أكتب مخرجات البرنامج التالي‬
cout<<
cout<<right;
cout<<setw(7)<<“Ali \n”;
Cout<<left;
Cout<<setw(11)<<“MMM”;
--------------------------------------float a=7.334;
cout<<setprecision(3);
cout<<fixed;
cout<<“a=”<<a;