Input Output Manipulation - Intro to Computer Programming - Lecture Slides, Slides of Computer Engineering and Programming

The key points in these lecture slides of intro to computer programming are given as:Input Output Manipulation, Library Header Files, Classes for Stream, Stream Manipulators, Select Output Precision, Stream Format States, Format Flags, Member Functions, End-Of-File Condition

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
I/O Manipulation
Lecture 27
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Input Output Manipulation - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

I/O Manipulation

Lecture 27

Docsity.com

Stream I/O Library Header Files

Note: There is no “.h” on standard header files. Be careful about “ using namespace std

  • iostream -- contains basic information required for all stream I/O operations
  • iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators
  • fstream -- contains information for performing file I/O operations
  • strstream -- contains information for performing in- memory I/O operations (i.e., into or from strings in memory)

Docsity.com

C++ Stream I/O -- Stream Manipulators

  • C++ provides various stream manipulators that perform formatting tasks.
  • Stream manipulators are defined in
  • These manipulators provide capabilities for
    • setting field widths,
    • setting precision,
    • setting and unsetting format flags,
    • flushing streams,
    • inserting a "newline" and flushing output stream,
    • skipping whitespace in input stream

Docsity.com

C++ Stream I/O -- Stream Manipulators

setprecision ( )

  • Select output precision, i.e., number of significant digits to be printed.
  • Example: cout << setprecision (2) ; // two significant digits setw ( )
  • Specify the field width (Can be used on input or output, but only applies to next insertion or extraction).
  • Example: cout << setw (4) ;// field is four positions wide

Docsity.com

Stream I/O Format State Flags

ios::showpoint when set, show trailing decimal point and zeros

ios::showpos when set, show the + sign before positive numbers

ios::basefield

ios::dec use base ten Docsity.com

Stream I/O Format State Flags

ios::floatfield

ios::fixed use fixed number of digits ios::scientific use "scientific" notation

ios::adjustfield

ios::left use left justification ios::right use right justification ios::internal left justify the sign, but right justify the value Docsity.com

I/O Stream Member Functions

.setf ( )

  • Allows the setting of an I/O stream format

flag.

  • Examples:

// To show the + sign in front of positive numbers cout.setf (ios::showpos) ; Docsity.com

I/O Stream Member Functions

.precision ( ) ;

  • Select output precision, i.e., number of significant digits to be printed.
  • Example:

cout.precision (2) ; // two significant digits

.width ( ) ;

  • Specify field width. (Can be used on input or output, but only applies to next insertion or Docsity.com

I/O Stream Member Functions

.clear ( ) ;

  • Normally used to restore a stream's state to

"good" so that I/O may proceed or resume on that stream.

  • Example:

cin.clear ( ) ; // allow I/O to resume on a "bad" // stream, in this case "cin",Docsity.com

Using Manipulators & Member

Functions

#include // No “.h” (standard header) #include // No “.h” (standard header) using namespace std; // To avoid “std::” int main ( ) { int a, b, c = 8, d = 4 ; float k ; char name[30] ; cout << "Enter your name" << endl ; cin.getline (name, 30) ; cout << "Enter two integers and a float " << endl ; cin >> a >> b >> k ;

Docsity.com

Example Program Output

Enter your name

R. J. Freuler

Enter two integers and a float

12 24 67.

Thank you, R. J. Freuler, you entered

12, 24, and  68

Thank you, R. J. Freuler, you entered

12, 24, and 67.85 Docsity.com

More Input Stream Member

Functions

.get ( ) ;

Example:

char ch ; ch = cin.get ( ) ; // gets one character from keyboard // & assigns it to the variable "ch"

.get (character) ;

Example: Docsity.com

More Input Stream Member

Functions

.getline (array_name, max_size) ;

Example:

char name[40] ; cin.getline (name, 40) ; // Gets up to 39 characters // and assigns the string to "name". A // null is inserted at the end of the string. Docsity.com

More Input Stream Member

Functions

.ignore ( ) ;

Ex:

cin.ignore ( ) ; // gets and discards 1 character

cin.ignore (2) ; // gets and discards 2 characters

cin.ignore (80, '\n') ; // gets and discards up to 80 Docsity.com