510 likes | 651 Views
Introduction to Programming. Lecture 36. #include <iostream.h> #include <fstream.h>. iomanip.h. cin.eof ( ) ; cin.fail ( ) ; cin.bad ( ) ; cin.good ( ) ; cin.clrear ( ) ;. Manipulators. Stream Manipulators. float PI = 3.1415926 ;. endl. cout << endl ;. cout << flush ;.
E N D
Introduction to Programming Lecture 36
cin.eof ( ) ; • cin.fail ( ) ; • cin.bad ( ) ; • cin.good ( ) ; • cin.clrear ( ) ;
Number System • Binary • Decimal • Octal • Hexadecimal
Example int i = 10 ; cout << i ;
Example #include <iostream.h> #include <iomanip.h> main ( ) { int i = 10 ; Output cout << oct << i << endl ; 12 cout << hex << i<< endl ; A cout << dec << i << endl ; 10 }
Example #include <iostream.h> #include <iomanip.h> main ( ) { int i = 5 ; cout << “The value of i is = ” ; cout << setw ( 4 ) << i << endl ; }
cout << setfill ( ‘*’ ) ; A Character
Example #include<iostream.h> #include<iomanip.h> Main ( ) { int i = 4000 ; cout << setfill ( ‘*’ ) << setw ( 10 ) << i << endl ; }
Example #include<iostream.h> #include<iomanip.h> main ( ) { float number = 6.67076632 ; cout << setprecision ( 2 ) << number << endl ; }
Example #define PI 3.1415926 main ( ) { cout << PI << endl ; cout << setprecision ( 2 ) << PI << endl ; }
Example #include <iostream.h> #include <iomanip.h> main ( ) { int x = 10 ; cout << setbase ( 8 ) << x <<endl ; cout << setbase ( 16 ) << x <<endl ; cout << setbase ( 10 ) << x <<endl ; cout << setbase ( 0 ) << x <<endl ; } Same as setbase (10)
Input Output state flags IOS Flags
Example #include <iostream.h> #include <iomanip.h> main ( ) { int i = 10 , j = 20 ; cout << setw ( 7 ) << i <<endl ; cout << j ; }
Formatting Manipulation • ios :: adjustfield • ios :: left • ios :: right • ios :: left | ios :: right , ios :: adjustfield
Formatting Manipulation cout.fill ( '0' ) ; cout << setw ( 10 ) << number << endl ;
7ff 0111 1111 1111
showbase cout.setf ( ios :: showbase ) ;
showbase cout.setf ( ios :: showbase ) ; cout.setf ( ios::dec , ios :: basefield ) ; cout << x << '\n' ; // Outputs 77 cout.setf ( ios :: oct , ios :: basefield ) ; cout << x << '\n' ; // Outputs 077 cout.setf ( ios :: hex , ios :: basefield ) ; cout << x << '\n' ; // Outputs 0x77
Scientific Notation 1.2334e+09 +/-