lecture #5: iostream functions دوال الإدخال والإخراج dr. hmood al-dossari king...
TRANSCRIPT
Lecture #5:iostream functions
واإلخراج اإلدخال دوال
Dr. Hmood Al-DossariKing 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
• ‘>>’ 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.get()
• 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: cin.ignore()
• 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.putback()
• peek function reads and returns the next character without extracting it.
• The syntax of cin statement with peek function:variable = istream.peek();
Input Functions: istream.peek()
Input Functions: putback and peek
• 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;
Input Functions: cin.clear()
Suppose the input is: 12 ROutput: 12 , 34
Suppose the input is: 19 56Output: 19 , 56
• التالي البرنامج مخرجات أكتب
int a = 23;
int b = 34;
cin>>a>>b;
cin.clear();
cout<<a<<“ , "<<b;
Activity 1 - 1نشاط -
Suppose the input is: R 12Output:
• setprecision function is used to control the output of floating-point numbers.
• a maximum of six decimal places for default output of floating-point.
• 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 Functions: setprecision()
Output:
2.38
• 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 Functions: fixed
Output:
2.38890
• 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 Functions: showpoint
Output:
2.00000
• 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”;
Output Functions: setw()
1 2 3 4 5 6 7 8 9 10 11 12
domH o
• 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’&’;
Output Functions: setfill()
1 2 3 4 5 6 7 8 9 10 11 12
domH o&&&&&& &
• 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”;
Output Functions: left function
1 2 3 4 5 6 7 8 9 10 11 12
doomH
• 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”;
Output Functions: right function
1 2 3 4 5 6 7 8 9 10 11 12
domH o
• التالي البرنامج مخرجات أكتب
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;
Activity 2 - 2نشاط -