Introduction to Programming Lecture 36 include iostream h

  • Slides: 51
Download presentation
Introduction to Programming Lecture 36

Introduction to Programming Lecture 36

#include <iostream. h> #include <fstream. h>

#include <iostream. h> #include <fstream. h>

iomanip. h

iomanip. h

cin. eof ( ) ; n cin. fail ( ) ; n cin. bad

cin. eof ( ) ; n cin. fail ( ) ; n cin. bad ( ) ; n cin. good ( ) ; n cin. clrear ( ) ; n

Manipulators

Manipulators

Stream Manipulators

Stream Manipulators

float PI = 3. 1415926 ;

float PI = 3. 1415926 ;

endl

endl

cout << endl ;

cout << endl ;

cout << flush ;

cout << flush ;

Manipulator With Arguments

Manipulator With Arguments

Inline Manipulator

Inline Manipulator

cout. flush ( ) ;

cout. flush ( ) ;

Number System Binary n Decimal n Octal n Hexadecimal n

Number System Binary n Decimal n Octal n Hexadecimal n

Example int i = 10 ; cout << i ;

Example int i = 10 ; cout << i ;

10

10

Example #include <iostream. h> #include <iomanip. h> main ( ) { int 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 }

White Space

White Space

WS Manipulator

WS Manipulator

setw

setw

Example #include <iostream. h> #include <iomanip. h> main ( ) { int i =

Example #include <iostream. h> #include <iomanip. h> main ( ) { int i = 5 ; cout << “The value of i is = ” ; cout << setw ( 4 ) << i << endl ; }

setfill

setfill

cout << setfill ( ‘*’ ) ; A Character

cout << setfill ( ‘*’ ) ; A Character

Example #include<iostream. h> #include<iomanip. h> Main ( ) { int i = 4000 ;

Example #include<iostream. h> #include<iomanip. h> Main ( ) { int i = 4000 ; cout << setfill ( ‘*’ ) << setw ( 10 ) << i << endl ; }

Set Precision Manipulator

Set Precision Manipulator

Example #include<iostream. h> #include<iomanip. h> main ( ) { float number = 6. 67076632

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

Example #define PI 3. 1415926 main ( ) { cout << PI << endl ; cout << setprecision ( 2 ) << PI << endl ; }

setbase

setbase

Example #include <iostream. h> #include <iomanip. h> main ( ) { int x =

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

Input Output state flags IOS Flags

width ( ) ;

width ( ) ;

cin. width ( 7 ) ; cout. width ( 10 ) ;

cin. width ( 7 ) ; cout. width ( 10 ) ;

cout. precision ( 2 ) ;

cout. precision ( 2 ) ;

Example #include <iostream. h> #include <iomanip. h> main ( ) { int i =

Example #include <iostream. h> #include <iomanip. h> main ( ) { int i = 10 , j = 20 ; cout << setw ( 7 ) << i <<endl ; cout << j ; }

Formatting Manipulation n n ios : : adjustfield ios : : left ios :

Formatting Manipulation n n ios : : adjustfield ios : : left ios : : right ios : : left | ios : : right , ios : : adjustfield

cout. setf ( ios : : left , ios : : adjustfield ) ;

cout. setf ( ios : : left , ios : : adjustfield ) ; Set Flag

cout. fill ( ‘*’ ) ;

cout. fill ( ‘*’ ) ;

cout. fill ( '0' ) ;

cout. fill ( '0' ) ;

Formatting Manipulation cout. fill ( '0' ) ; cout << setw ( 10 )

Formatting Manipulation cout. fill ( '0' ) ; cout << setw ( 10 ) << number << endl ;

cout. setf ( ios : : hex ) ;

cout. setf ( ios : : hex ) ;

7 ff 0111 1111

7 ff 0111 1111

showbase

showbase

showbase cout. setf ( ios : : showbase ) ;

showbase cout. setf ( ios : : showbase ) ;

showbase cout. setf ( ios : : showbase ) ; cout. setf ( ios:

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 0 x 77

ios : : scientific

ios : : scientific

cout. setf ( ios : : scientific ) ;

cout. setf ( ios : : scientific ) ;

Scientific Notation +/- +09 1. 2334 e

Scientific Notation +/- +09 1. 2334 e

Fixed Point Notation

Fixed Point Notation

ios : : fixed

ios : : fixed

ios : : uppercase

ios : : uppercase

What we learnt so far. . Input Output Stream n And their Manipulations in

What we learnt so far. . Input Output Stream n And their Manipulations in C++ n