managing console

39
MANAGING CONSOLE I/O OPERATIONS Submitted by:- Namita Pandey 2011BTechece020

Upload: shiva-saxena

Post on 22-May-2015

2.590 views

Category:

Education


3 download

DESCRIPTION

This is a precise ppt on managing consoles I/O operations. i hope you will find it good and useful.

TRANSCRIPT

Page 1: Managing console

MANAGING CONSOLE I/OOPERATIONS

Submitted by:- Namita Pandey 2011BTechece020 Shiva Johari 2011BTechcse015

Pritam Kalwaniya 2011BTechcse008

Page 2: Managing console

OUTLINE

Introduction

Streams & Stream Classes

Unformatted Input Output Operations

Formatted Console Input Output Operation

Formatting Flags, Bit fields and setf()

Designing Our Own Manipulators

Page 3: Managing console

INTRODUCTION

Managing Console I/O Operations

INPUT & OUTPUT

C++ supports a rich set of I/O operations

C++ uses the concept of stream & stream classes

Page 4: Managing console

STREAMS ???o A sequence of bytes o An interface between program and device

INPUTDEVICE

INSERTION INTO OUTPUTSTREAM

EXTRACTION FROM INPUTSTREAM

PROGRAM

OUTPUTDEVICE

INPUTSTREAM

OUTPUTSTREAM

Page 5: Managing console

STREAM CLASSES ??? C++ contains a hierarchy of classes that are used to define various streams

ios

Ostream_withassign

iostream

ostreamstreambuf

Iostream_withassign

istream

Istream_withassign

INPUTPOINTER

OUTPUT

Page 6: Managing console

UNFORMATTED I/O OPERATIONS

Overloaded operators >> and <<

get() and put() functions

getline() and write() functions

Page 7: Managing console

FORMATTED I/O OPERATIONS

C++ supports a number of features which can be used for formatting the output. These features include :-

ios class functions Manipulators User-defined Manipulators

Page 8: Managing console

ios format functions

FUNCTION

Width()

Precision()

Fill()

Setf()

Unsetf()

TASK

To specify the required field size for displaying the output value

To specify the digits to be displayed after decimal point of a float value

To specify a character that is used to fill the unused portion of a field

To specify format flags that can control the form of output display

To clear the flags specified

Page 9: Managing console

MANIPULATORS

MANIPULATORS

setw()

setprecision()

setfill()

setiosflags()

resetiosflags()

EQUIVALENT IOS FUNCTION

width()

precision()

fill()

setf()

unset()

Page 10: Managing console

Setting Width : width()

cout.width(w);

cout.width(5);cout<<543<<12<<“\n”;

cout.width(5);`cout<<543;cout.width(5);cout<<12<<“\n”;

4 35 21

5 4 3 21

Page 11: Managing console

#include<iostream.h>

int main(){ int item[4] =

{10,8,12,15}; int cost[4] =

{75,100,60,99}; cout.width(5); cout << “ITEMS”; cout.width(8); cout << “COST”; cout.width(15); cout << value<<“\

n”; sum =sum + value; int sum = 0; for(int i=0; i<4;i++) { cout.width(5);

cout << items[i];

cout.width(8); cout << cost[i];

int value = items[i] * cost[i];

cout.width(15); cout << value <<“\

n”; sum =sum + value;} cout << “\n Grand

Total = “;

cout.width(2); cout << sum <<“\

n”;

return 0;

}

Page 12: Managing console

Output

ITEMS COST TOTAL VALUE 10 15 150 8 100 800 12 60 720 15 99 1485

Page 13: Managing console

Setting Precision : precision()

cout.precision(n);

cout.precision(3);cout<<sqrt(2)<<“\n”;cout<<3.14159<<“\n”;cout<<2.50032<<“\n”;

1.1413.1422.5

Page 14: Managing console

#include<iostream.h>#include<conio.h>void main(){

float pi=22.0/7.0;int I;cout<<“Value of pi :\n “;for(i=1;i<=10;i++){

cout.width(i+1);cout.precision(i);cout<<pi<<“\n”;

}}

Page 15: Managing console

Value of pi :3.13.143.1433.14293.142863.1428573.14285713.142857073.1428570753.1428570747

OUTPUT

Page 16: Managing console

cout.fill(ch);

cout.fill(‘*’);cout.width(10);cout<<5250<<“\n”;

* * ** * * 0525

Filling & Paddling : fill()

Page 17: Managing console

#include<iostream.h>#include<conio.h>void main(){cout.fill(‘<‘);

cout.precision(3);for(int n=1;n<=6;n++){

cout.width(5);cout<<n;cout.width(10);cout<<1.0/

float(n)<<“\n”;if(n==3) cout.fill(‘>’);

}

cout<<“\n Paddling Changed\n\n”;cout.fill(‘#’);cout.width(15);cout<<12.345678<<“\n”;

return 0;}

Page 18: Managing console

<<<<1<<<<<<<<<1<<<<2<<<<<<<0.5<<<<3<<<<<<<0.3>>>>4>>>>>>0.25>>>>5>>>>>>>0.2>>>>6>>>>>0.167

Paddling Changed

#########12.346

OUTPUT

Page 19: Managing console

Formatting Flags, Bit-fields and setf()

cout.setf(arg1,arg2)

arg1 - formatting flags defined in the class ios

arg2 - it specifies the group to which the formatting flags belongs

Page 20: Managing console

Flags & Bit-fields for setf() function

Left-justified output

Right-justified output

Padding after sign or base Indicator (like

+##20)

Scientific Notation

Fixed Point notation

Decimal Base

ios::left

ios::right

ios::internal

ios::scientific

ios::fixed

ios::dec

ios::adjustfield

ios::adjustfield

ios::adjustfield

ios::floatfield

ios::floatfield

ios::basefield

FORMAT REQUIRED FLAG (ARG1) BIT-FIELD (ARG2)

Page 21: Managing console

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

cout.setf(ios::fixed, ios::floatfield); float x=1234.67 ; cout<<x<<endl;

cout.setf(ios::scientific, ios::floatfield); x=.123467 ; cout<<x;

getch();

}

Page 22: Managing console

OUTPUT

1234.668234

1.234672e-01

Page 23: Managing console

#include<iostream.h>#include<conio.h>void main(){

int num;cout<<“enter an integer value”;cin>>num;

cout<<“The hexadecimal, octal and decimal representation is : ”;

cout.setf(ios::hex, ios::basefield)cout<<num<<“, “;

cout.setf(ios::oct, ios::basefield) cout<<num<<“, “;

cout.setf(ios::dec, ios::basefield)cout<<“ and “<<num<<“

respectively”;}

Page 24: Managing console

Enter an integer value : 92

The hexadecimal, octal and decimal representation of 92 is: 5c, 134 and 92 respectively.

OUTPUT

Page 25: Managing console

DESIGNING OUR OWN MANIPULATORS

ostream & manipulator (ostream & output){………………………… (code)……………

return output;}

Page 26: Managing console

So, what is the main conclusion ??

Page 27: Managing console

BIBLIOGRAPHY_____________

We have taken all the basic ideas about the concepts from the book “OBJECT ORIENTED TECHNIQUES” – by E. Balagurusamy

Images are made in Ms- Paint

& every thing is accompanied by ideas of our own

Page 28: Managing console

Any Queries ???

Page 29: Managing console

QUI

Z

TIM

E

Page 30: Managing console

Q.1The function of istream class is to :

a) inherit the properties of ios

b)Declares input functions such as get(), getline(), read() etc.

c)Contains overloaded extraction operator >>

d)All of the above

Page 31: Managing console

Q2.The function of streambuf is to :

a) provides an interface to physical devices through buffers

b)Can’t act as a base for filebuf class used for ios files

c) Declares constants and functions that are necessary for handling formatted i/p and o/p operations

d)None of the above

Page 32: Managing console

Q3.A stream is a sequence of ___________.

a) Bytes

b) Files

c) Manipulators

d) None of the above

Page 33: Managing console

Q4.Which are the member functions of ios class :

a)precision()

b)width()

c)fill()

d)All the above

Page 34: Managing console

Q5.What will be the output of following :

cout.fill(‘*’);cout.precision(3);cout.setf(ios::internal, ios::adjustfield);cout.setf(ios::scientific, ios::floatfield);cout.width(15);

cout<<-12.34567<<“\n”;

(.A

D.)

B.)

(.C

-*****1.235e+01

-*****.1235e+02-*********1.236

-******1.235e+01

Page 35: Managing console

Q6.a) The __________ operator is overloaded in the istream

class

a) Insertion

b) >>

c) <<

d) None of the above

Page 36: Managing console

Q7.Which Class is needed to be virtual in this case :

ios

Ostream_withassign

iostream

ostreamstreambuf

Iostream_withassign

istream

Istream_withassign

INPUTPOINTER

OUTPUT

a.) iostreamb.) iosc.) istream or ostreamd.) no one is required

Page 37: Managing console

True / False

Q8.The header file iomanip can be used in place of iostream ??

Q9. programmer can’t define a manipulator that could represent a set of formatted functions ??

Page 38: Managing console

Q10.What is the default precision value ??

a.) 0 b.) 4

c.) 6 d.) 3

Page 39: Managing console

THANKSA

LOT