I/O and Program Control Statements - PowerPoint PPT Presentation

About This Presentation
Title:

I/O and Program Control Statements

Description:

I/O and Program Control Statements Dick Steflik – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 12
Provided by: Pari170
Category:

less

Transcript and Presenter's Notes

Title: I/O and Program Control Statements


1
I/O and Program Control Statements
  • Dick Steflik

2
Overloading
  • C provides two types of overloading
  • function overloading
  • the ability to use the same function name to have
    multiple definitions
  • operator overloading
  • the ability to define multiple definitions for
    any (almost) of the graphical symbols used for
    operators (,-,,ltlt,gtgt)

3
But First... I/O
  • To enable your program to do input and output you
    must include the IOSTREAM header file
  • if you are using namespace std
  • include ltiostreamgt
  • if you are not using a namespace
  • include ltiostream.hgt
  • or tell the compiler explicitly
  • stdcout ltlt ...

4
more on I/O...
  • Once iostream has been included we can use the
    predefined objects cout, cin and cerr.
  • cout, cin and cerr are predefined objects of
    class iostream they are not I/O statements in
    the sense of program statements, they are objects
    that we will invoke methods on.

5
more on I/O...
  • The class iostream allows us to define something
    called a stream, a stream is nothing more than a
    sequence of bytes
  • in the case of cin it is the sequence of bytes
    coming from sysin (the keyboard)
  • in the case of cout it is the sequence of bytes
    being transferred to sysout (the display)

6
the insertion operator ltlt...
  • For iostream the shift left operator ltlt has
    been overloaded and is called insertion
  • ltlt is used to insert (write) characters into the
    stream being used
  • cout ltlt Dick Steflik
  • inserts Dick Steflik into the console output
    stream
  • cout ltlt your grade is ltlt grade

7
the extraction operator gtgt...
  • For iostream the shift right operator gtgt has
    been overloaded and is called extraction
  • gtgt is used to extract (read) information from the
    console input stream (keyboard) and assign it to
    program variables
  • int x cin gtgt x
  • read an integer from the input stream into the
    variable x

8
iomanip.h
  • iomanip.h provides a number of stream
    manipulators that when placed inline with our
    cin/cout method calles affect the way the
    commands are handled
  • setbase(n) will set the base to be used for
    insertion and extraction to n
  • cout ltlt setbase(8) ltlt n // will cause n to be
    printed out as an octal number
  • cout ltlt oct ltlt n // will accomplish the same
    thing
  • cout ltlt hex ltlt n // will print n as a
    hexadecimal number
  • cout ltltdec ltlt n // will print n as a decimal
    number
  • setw or width - will set the field width of
    subsequent fields
  • cout .setw(10) coutltlt n // n will be printed
    right justified in a field 10 char wide
  • cout ltlt setw(12) ltlt n // same thing

9
Formatting flags
  • setf, unsetf and flags control I/O flag settings
  • iosskipws - skip white space on input stream
  • iosleft - left justify output in a field, pad
    to right (if needed)
  • iosright - right justify in a field, pad on
    left (if needed)
  • iosinternal - left justify the sign, right
    justify value
  • iosdec - treat integers as base 10
  • iosoct - treat integers as octal
  • ioshex - treat integers as hexadecimal
  • iosshowbase - display the base ahead of number
    (0 for octal, 0x for hex)
  • iosshowpoint - display floating point numbers
    with a decimal point
  • iosfixed - display floating point numbers with
    specified number of decimal digits
  • iosuppercase - show upper case (ABCDEF) letters
    in hexadecimal numbers
  • iosscientific - show floating point numbers in
    scientific notation

10
Using Formatting flags
  • The flags member function must specify a value
    for each formatting flag (seldom used)
  • The setf member function and the stream
    manipulator setiosflags are used to set one or
    more formating flags likewise unsetf and
    resetioflags will reset one or more formatting
    flags.
  • cout ltlt setf( ioshex iosuppercase) ltlt x
  • displays x in hexadecinal format with uppercase
    letters for letters

11
Deitel Deitel
  • Review Chapter 11 of the Deitel Deitel C How
    to Program book for a good discussion on Stream
    I/O
Write a Comment
User Comments (0)
About PowerShow.com