CS201- Introduction to Programming- Lecture 36

Preview:

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 36 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Citation preview

Introduction to Introduction to ProgrammingProgramming

Lecture 36Lecture 36

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

iomanip.iomanip.hh

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

ManipulatorManipulatorss

Stream Stream ManipulatorManipulator

ss

float PI = 3.1415926 float PI = 3.1415926 ;;

endlendl

cout << endl cout << endl ;;

cout << flush cout << flush ;;

Manipulator Manipulator With With

ArgumentsArguments

Inline Inline ManipulatManipulat

oror

cout.flush cout.flush ( ) ;( ) ;

Number SystemNumber System BinaryBinary DecimalDecimal OctalOctal HexadecimalHexadecimal

Example Example

int i = 10 ;int i = 10 ;cout << i ;cout << i ;

1010

#include <iostream.h>#include <iostream.h>#include <iomanip.h>#include <iomanip.h>main ( )main ( ){{

int i = 10 ;int i = 10 ; OutputOutputcout << oct << i << endl ;cout << oct << i << endl ; 12 12cout << hex << i<< endl ;cout << hex << i<< endl ; A Acout << dec << i << endl ;cout << dec << i << endl ; 10 10

}}

ExampleExample

White White SpaceSpace

WS WS ManipulatManipulat

oror

setwsetw

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

#include <iomanip.h>#include <iomanip.h>

main ( )main ( )

{{

int i = 5 ;int i = 5 ;

cout << “The value of i is = ” ;cout << “The value of i is = ” ;

cout << setw ( 4 ) << i << endl ;cout << setw ( 4 ) << i << endl ;

}}

ExampleExample

setfilsetfilll

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

A Character

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

#include<iomanip.h>#include<iomanip.h>

Main ( )Main ( )

{{

int i = 4000 ;int i = 4000 ;

cout << setfill ( ‘*’ ) << setw ( 10 ) << i << cout << setfill ( ‘*’ ) << setw ( 10 ) << i << endl ;endl ;

}}

ExampleExample

Set Precision Set Precision ManipulatorManipulator

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

#include<iomanip.h>#include<iomanip.h>

main ( )main ( )

{{

float number = 6.67076632 ;float number = 6.67076632 ;cout << setprecision ( 2 ) << number cout << setprecision ( 2 ) << number

<< endl ;<< endl ;

}}

ExampleExample

#define PI 3.1415926#define PI 3.1415926

main ( )main ( )

{{

cout << PI << endl ;cout << PI << endl ;

cout << setprecision ( 2 ) << PI << endl ;cout << setprecision ( 2 ) << PI << endl ;

}}

ExampleExample

setbassetbasee

#include <iostream.h>#include <iostream.h>#include <iomanip.h>#include <iomanip.h>main ( )main ( ){{

int x = 10 ;int x = 10 ;cout << setbase ( 8 ) << x <<endl cout << setbase ( 8 ) << x <<endl

;;cout << setbase ( 16 ) << x <<endl cout << setbase ( 16 ) << x <<endl

;; cout << setbase ( 10 ) << x <<endl ;cout << setbase ( 10 ) << x <<endl ; cout << setbase ( 0 ) << x <<endl ;cout << setbase ( 0 ) << x <<endl ;

}}

ExampleExample

Same as setbase (10)

Input Output state Input Output state flagsflags

IOS FlagsIOS Flags

width width ( ) ;( ) ;

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

cout.precision cout.precision ( 2 ) ;( 2 ) ;

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

#include <iomanip.h>#include <iomanip.h>

main ( )main ( )

{{

int i = 10 , j = 20 ;int i = 10 , j = 20 ;

cout << setw ( 7 ) << i cout << setw ( 7 ) << i <<endl ;<<endl ;

cout << j ;cout << j ;

}}

ExampleExample

ios :: adjustfieldios :: adjustfield ios :: leftios :: left ios :: rightios :: right ios :: left | ios :: right , ios :: ios :: left | ios :: right , ios ::

adjustfieldadjustfield

Formatting Manipulation

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

Set FlagSet Flag

cout.fill ( ‘*’ ) cout.fill ( ‘*’ ) ;;

cout.fill ( '0' ) ;cout.fill ( '0' ) ;

Formatting Formatting ManipulationManipulation

cout.fill ( '0' ) ;cout.fill ( '0' ) ;

cout << setw ( 10 ) << number << endl ;cout << setw ( 10 ) << number << endl ;

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

7ff7ff0111 1111 1111

showbasshowbasee

showbaseshowbase

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

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

cout.setf ( ios::dec , ios :: basefield ) ; cout.setf ( ios::dec , ios :: basefield ) ;

cout << x << '\n' ; // Outputs 77cout << x << '\n' ; // Outputs 77

cout.setf ( ios :: oct , ios :: basefield ) ;cout.setf ( ios :: oct , ios :: basefield ) ;

cout << x << '\n' ; // Outputs 077 cout << x << '\n' ; // Outputs 077

cout.setf ( ios :: hex , ios :: basefield ) ; cout.setf ( ios :: hex , ios :: basefield ) ;

cout << x << '\n' ; // Outputs 0x77cout << x << '\n' ; // Outputs 0x77

showbaseshowbase

ios :: ios :: scientificscientific

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

Scientific Scientific NotationNotation

1.2334e1.2334e+09+09

+/-+/-

Fixed Point Fixed Point NotationNotation

ios :: fixedios :: fixed

ios :: ios :: uppercaseuppercase

What we learnt so far..What we learnt so far..

Input Output Input Output StreamStream

And their And their Manipulations in C+Manipulations in C+++

Recommended