CS 211:
Introduction to Computer Programming II
| Instructor: Brian M. Dennis | |
| Teaching Assistants: | |
| Tom Lechner, Bin Lin, Rachel Goldsborough | |
| http://www.cs.northwestern.edu/~bmd/cs211/ | |
| Already using them | ||
| cout << "foo"; | ||
| cin >> some_int; | ||
| Today, more sophisticated uses | ||
| Participate in a hierarchy of classes | ||
| Forward reference inheritance | ||
| 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, | |||
| istream cont | ||
| ignore | ||
| putback | ||
| peek | ||
| #include <iostream> | |
| using std::cout; | |
| using std::cin; | |
| using std::endl; | |
| int main() { | |
| const int SIZE = 80; | |
| char buf1[SIZE]; | |
| char buf2[SIZE]; | |
| cout << "Enter a sentence:" << endl; | |
| cin >> buf1; | |
| cout << "\nThe string read with cin was:" << endl | |
| << buf1 << endl << endl; |
| cin.get(buf2, SIZE); | |
| cout << "The string read with cin.get was:" << endl | |
| << buf2 << endl; | |
| cin.getline(buf2, SIZE); | |
| cout << "\nThe sentence read with getline is:" << endl | |
| << buf2 << endl; | |
| return 0; | |
| } |
| output | ||
| write, member function | ||
| cout.write(buf); | ||
| cout.write(buf, size) | ||
| input | ||
| read, member function | ||
| cout.read(buf, sz); | ||
| 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 | ||
| 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 | ||
| Reading | ||
| 12.1 – 12.7 | ||