CS 211: Introduction to Computer Programming II - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CS 211: Introduction to Computer Programming II

Description:

Participate in a hierarchy of classes. Forward ... clog. Templated. Datatypes. Stream member functions. ostream. put, takes a char, returns ostream ref ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 12
Provided by: briand8
Category:

less

Transcript and Presenter's Notes

Title: CS 211: Introduction to Computer Programming II


1
CS 211Introduction to Computer Programming II
  • Instructor Brian M. Dennis
  • Teaching Assistants
  • Tom Lechner, Bin Lin, Rachel Goldsborough
  • http//www.cs.northwestern.edu/bmd/cs211/

2
Todays Topics
  • Wrapping up the BitSet class
  • C stream I/O

3
Streams
  • Already using them
  • cout ltlt "foo"
  • cin gtgt some_int
  • Today, more sophisticated uses
  • Participate in a hierarchy of classes
  • Forward reference inheritance

4
Stream Hierarchy
Templated Datatypes
basic_ios
basic_ostream
basic_istream
cin
cout
cerr
basic_iostream
clog
5
Stream member functions
  • ostream
  • put, takes a char, returns ostream ref
  • istream
  • get, no args returns next char
  • get, char ref stores next char
  • get, char, size, delimeter
  • up to terminator or buf size
  • getline, no args,

6
Stream member functions
  • istream cont
  • ignore
  • putback
  • peek

7
Stream member functions
  • include ltiostreamgt
  • using stdcout
  • using stdcin
  • using stdendl
  • int main()
  • const int SIZE 80
  • char buf1SIZE
  • char buf2SIZE
  • cout ltlt "Enter a sentence" ltlt endl
  • cin gtgt buf1
  • cout ltlt "\nThe string read with cin was" ltlt
    endl
  • ltlt buf1 ltlt endl ltlt endl

8
Stream member functions
  • cin.get(buf2, SIZE)
  • cout ltlt "The string read with cin.get was" ltlt
    endl
  • ltlt buf2 ltlt endl
  • cin.getline(buf2, SIZE)
  • cout ltlt "\nThe sentence read with getline is"
    ltlt endl
  • ltlt buf2 ltlt endl
  • return 0

9
Unformatted I/O
  • output
  • write, member function
  • cout.write(buf)
  • cout.write(buf, size)
  • input
  • read, member function
  • cout.read(buf, sz)

10
Stream manipulators
  • Integer output base
  • dec, oct, hex manip
  • setbase member function
  • Floating point precision
  • of right digits displayed
  • setprecision manip
  • precision member function
  • Field manipulations
  • width member func, setw manip
  • skipws, noskipws
  • left, right
  • showbase, noshowbase
  • showpoint, noshowpoint
  • scientific, fixed

11
Stream flags
  • cin.eof()
  • Any input left?
  • cin.fail()
  • Error on input
  • cin.bad()
  • major error
  • cin.good()
  • bad, fail, eof all off
  • cout.rdstate()
  • Get flags as an integer
  • cin.clear()
  • reset flags
  • operator!
  • test badbit, failbit

12
Thats a Wrap
  • Reading
  • 12.1 12.7
Write a Comment
User Comments (0)
About PowerShow.com