0% found this document useful (0 votes)
116 views51 pages

Introduction To Programming

This document discusses input/output stream manipulation in C++. It covers stream manipulators like endl, setw, setprecision that control formatting of output. It shows examples of using manipulators to set width, precision, base of number systems. It also covers I/O state flags like setf that control formatting by setting flags for number base, fill character, and alignment. The document provides an introduction to stream manipulation and formatting concepts in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views51 pages

Introduction To Programming

This document discusses input/output stream manipulation in C++. It covers stream manipulators like endl, setw, setprecision that control formatting of output. It shows examples of using manipulators to set width, precision, base of number systems. It also covers I/O state flags like setf that control formatting by setting flags for number base, fill character, and alignment. The document provides an introduction to stream manipulation and formatting concepts in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 51

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 ;
Manipulator
With Arguments
Inline
Manipulator
cout.flush ( ) ;
Number System
 Binary
 Decimal

 Octal

 Hexadecimal
Example

int i = 10 ;
cout << i ;
10
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
WS
Manipulator
setw
Example
#include <iostream.h>
#include <iomanip.h>
main ( )
{
int i = 5 ;
cout << “The value of i is = ” ;
cout << setw ( 4 ) << i << endl ;
}
setfill
cout << setfill ( ‘*’ ) ;

A Character
Example
#include<iostream.h>
#include<iomanip.h>
Main ( )
{
int i = 4000 ;
cout << setfill ( ‘*’ ) << setw ( 10 ) << i << endl ;
}
Set Precision
Manipulator
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 ;
}
setbase
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
width ( ) ;
cin.width ( 7 ) ;
cout.width ( 10 ) ;
cout.precision ( 2 ) ;
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
cout.setf ( ios :: left , ios :: adjustfield ) ;

Set Flag
cout.fill ( ‘*’ ) ;
cout.fill ( '0' ) ;
Formatting Manipulation

cout.fill ( '0' ) ;
cout << setw ( 10 ) << number << endl ;
cout.setf ( ios :: hex ) ;
7ff
0111 1111 1111
showbase
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
ios :: scientific
cout.setf ( ios :: scientific ) ;
Scientific Notation
+/-

1.2334e +09
Fixed Point Notation
ios :: fixed
ios :: uppercase
What we learnt so far..

 Input Output Stream


 And their
Manipulations in C++

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy