Download Input Output Manipulation - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!
I/O Manipulation
Lecture 27
Docsity.com
Stream I/O Library Header Files
Note: There is no โ.hโ on standard header files. Be careful about โ using namespace std โ
- iostream -- contains basic information required for all stream I/O operations
- iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators
- fstream -- contains information for performing file I/O operations
- strstream -- contains information for performing in- memory I/O operations (i.e., into or from strings in memory)
Docsity.com
C++ Stream I/O -- Stream Manipulators
- C++ provides various stream manipulators that perform formatting tasks.
- Stream manipulators are defined in
- These manipulators provide capabilities for
- setting field widths,
- setting precision,
- setting and unsetting format flags,
- flushing streams,
- inserting a "newline" and flushing output stream,
- skipping whitespace in input stream
Docsity.com
C++ Stream I/O -- Stream Manipulators
setprecision ( )
- Select output precision, i.e., number of significant digits to be printed.
- Example: cout << setprecision (2) ; // two significant digits setw ( )
- Specify the field width (Can be used on input or output, but only applies to next insertion or extraction).
- Example: cout << setw (4) ;// field is four positions wide
Docsity.com
Stream I/O Format State Flags
ios::showpoint when set, show trailing decimal point and zeros
ios::showpos when set, show the + sign before positive numbers
ios::basefield
ios::dec use base ten Docsity.com
Stream I/O Format State Flags
ios::floatfield
ios::fixed use fixed number of digits ios::scientific use "scientific" notation
ios::adjustfield
ios::left use left justification ios::right use right justification ios::internal left justify the sign, but right justify the value Docsity.com
I/O Stream Member Functions
.setf ( )
- Allows the setting of an I/O stream format
flag.
// To show the + sign in front of positive numbers cout.setf (ios::showpos) ; Docsity.com
I/O Stream Member Functions
.precision ( ) ;
- Select output precision, i.e., number of significant digits to be printed.
- Example:
cout.precision (2) ; // two significant digits
.width ( ) ;
- Specify field width. (Can be used on input or output, but only applies to next insertion or Docsity.com
I/O Stream Member Functions
.clear ( ) ;
- Normally used to restore a stream's state to
"good" so that I/O may proceed or resume on that stream.
cin.clear ( ) ; // allow I/O to resume on a "bad" // stream, in this case "cin",Docsity.com
Using Manipulators & Member
Functions
#include // No โ.hโ (standard header) #include // No โ.hโ (standard header) using namespace std; // To avoid โstd::โ int main ( ) { int a, b, c = 8, d = 4 ; float k ; char name[30] ; cout << "Enter your name" << endl ; cin.getline (name, 30) ; cout << "Enter two integers and a float " << endl ; cin >> a >> b >> k ;
Docsity.com
Example Program Output
Enter your name
R. J. Freuler
Enter two integers and a float
12 24 67.
Thank you, R. J. Freuler, you entered
12, 24, and ยย 68
Thank you, R. J. Freuler, you entered
12, 24, and ยยย67.85 Docsity.com
More Input Stream Member
Functions
.get ( ) ;
Example:
char ch ; ch = cin.get ( ) ; // gets one character from keyboard // & assigns it to the variable "ch"
.get (character) ;
Example: Docsity.com
More Input Stream Member
Functions
.getline (array_name, max_size) ;
Example:
char name[40] ; cin.getline (name, 40) ; // Gets up to 39 characters // and assigns the string to "name". A // null is inserted at the end of the string. Docsity.com
More Input Stream Member
Functions
.ignore ( ) ;
Ex:
cin.ignore ( ) ; // gets and discards 1 character
cin.ignore (2) ; // gets and discards 2 characters
cin.ignore (80, '\n') ; // gets and discards up to 80 Docsity.com