Professional Documents
Culture Documents
Type safe
I/O sensitive to data type Error if types do not match
High-level I/O
Formatted Bytes grouped (into integers, characters, strings, etc.) Good for most I/O needs
2003 Prentice Hall, Inc. All rights reserved.
<iomanip.h>
Formatted I/O with parameterized stream manipulators
<fstream.h>
File processing operations
12.2.3 Stream Input/Output Classes and Objects iostream library has class templates for I/O
basic_istream (stream input) basic_ostream (stream output) basic_iostream (stream input and output)
12.2.3 Stream Input/Output Classes and Objects Templates "derive" from basic_ios
basic_ios
basic_istream
basic_ostream
basic_iostream
cin
istream object Connected to standard input (usually keyboard) cin >> grade;
Compiler determines data type of grade Calls proper overloaded operator No extra type information needed
10
cerr, clog
ostream objects Connected to standard error device cerr outputs immediately clog buffers output
Outputs when buffer full or flushed Performance advantage (discussed in OS classes)
2003 Prentice Hall, Inc. All rights reserved.
11
12.2.3 Stream Input/Output Classes and Objects C++ file processing similar
Class templates
basic_ifstream (file input) basic_ofstream (file output) basic_fstream (file I/O)
12
basic_istream
basic_ostream
basic_ifstream
basic_iostream
basic_ofstream
basic_fstream
13
Formatted and unformatted Standard data types (<<) Characters (put function)
Integers (decimal, octal, hexadecimal) Floating point numbers
Various precision, forced decimal points, scientific notation
14
Problem
<< overloaded to print null-terminated string Solution: cast to void *
Use whenever printing value of a pointer Prints as a hex (base 16) number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Fig. 12.3: fig12_03.cpp // Printing the address stored in a char * variable. #include <iostream> using std::cout; using std::endl; int main() { char *word = "test";
15
Outline
fig12_03.cpp (1 of 1)
// display value of char *, then display // static_cast to void * cout << "Value of word is: " << word << endl << "Value of static_cast< void * >( word ) is: " << static_cast< void * >( word ) << endl; return 0; } // end main
To print the value of the pointer, we must cast to a void *. Otherwise, the string is printed. value of char *
fig12_03.cpp output (1 of 1)
Value of word is: test Value of static_cast< void *>( word ) is: 0046C070
16
May be cascaded
cout.put( 'A' ).put( '\n' ); Dot operator (.) evaluates left-to-right
17
>> operator
Normally skips whitespace (blanks, tabs, newlines)
Can change this
18
End-of-file
Indicates end of input
ctrl-z on IBM-PCs ctrl-d on UNIX and Macs
cin.eof()
Returns 1 (true) if EOF has occurred
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.4: fig12_04.cpp // Using member functions get, put and eof. #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int character; // use int, because char cannot represent EOF // prompt user to enter line of text cout << "Before input, cin.eof() is " << cin.eof() << endl Function get (with no << "Enter a sentence followed by end-of-file:" << endl;
19
Outline
fig12_04.cpp (1 of 2)
arguments) returns a single input, unless // use get to read each character; usecharacter put to display it EOF encountered. while ( ( character = cin.get() ) != EOF )
cout.put( character ); // display end-of-file character cout << "\nEOF in this system is: " << character << endl; cout << "After input, cin.eof() is " << cin.eof() << endl; return 0;
26 27
20
} // end main
Outline
fig12_04.cpp (2 of 2) fig12_04.cpp output (1 of 1)
Before input, cin.eof() is 0 Enter a sentence followed by end-of-file: Testing the get and put member functions Testing the get and put member functions ^Z
21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.5: fig12_05.cpp // Contrasting input of a string via cin and cin.get. #include <iostream> using std::cout; using std::cin; using std::endl; int main() { // create two char arrays, each with 80 elements const int SIZE = 80; char buffer1[ SIZE ]; char buffer2[ SIZE ]; // use cin to input characters cout << "Enter a sentence:" << endl; cin >> buffer1;
22
Outline
fig12_05.cpp (1 of 2)
cin will only read until the first whitespace. into buffer1
// display buffer1 contents cout << "\nThe string read with cin was:" << endl No delimiter << buffer1 << endl << endl; // use cin.get to input characters into buffer2 cin.get( buffer2, SIZE );
26 27 28 29 30 31 32 33
23
// display buffer2 contents cout << "The string read with cin.get was:" << endl << buffer2 << endl; return 0; } // end main
Outline
fig12_05.cpp (2 of 2) fig12_05.cpp output (1 of 1)
The string read with cin.get was: string input with cin and cin.get
24
Removes delimiter from input stream Puts null character at end of array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Fig. 12.6: fig12_06.cpp // Inputting characters using cin member function getline. #include <iostream> using std::cout; using std::cin; using std::endl; int main() { const int SIZE = 80; char buffer[ SIZE ]; // create array of 80 characters // input characters in buffer via cin function getline cout << "Enter a sentence:" << endl; cin.getline( buffer, SIZE ); // display buffer contents cout << "\nThe sentence entered is:" << endl << buffer << endl; return 0;
25
Outline
fig12_06.cpp (1 of 1)
} // end main
Enter a sentence: Using the getline member function The sentence entered is: Using the getline member function
26
Outline
fig12_06.cpp output (1 of 1)
27
putback()
Puts character obtained by get() back on stream
peek()
Returns next character in stream, but does not remove
28
29
12.5 Unformatted I/O using read, write and gcount Unformatted I/O
read (member of istream)
Input raw bytes into character array If not enough characters read, failbit set gcount() returns number of characters read by last operation
write (ostream)
Output bytes from character array Stops when null character found char buffer[] = "HAPPY BIRTHDAY"; cout.write( buffer, 10 ); Outputs first 10 characters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.7: fig12_07.cpp // Unformatted I/O using read, gcount and write. #include <iostream> using std::cout; using std::cin; using std::endl; int main() { const int SIZE = 80; char buffer[ SIZE ]; // create array of 80 characters
30
Outline
fig12_07.cpp (1 of 1)
Get 20 characters from input // use function read to input charactersstream. into buffer Display the proper cout << "Enter a sentence:" << endl; number of characters using cin.read( buffer, 20 ); write and gcount.
// use functions write and gcount to display buffer characters cout << endl << "The sentence entered was:" << endl; cout.write( buffer, cin.gcount() ); cout << endl;
return 0;
} // end main
Enter a sentence: Using the read, write, and gcount member functions The sentence entered was: Using the read, writ
31
Outline
fig12_07.cpp output (1 of 1)
32
33
12.6.1 Integral Stream Base: dec, oct, hex and setbase Integers normally base 10 (decimal)
Stream manipulators to change base
hex (base 16) oct (base 8) dec (resets to base 10) cout << hex << myInteger
setbase(newBase)
One of 8, 10, or 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.8: fig12_08.cpp // Using stream manipulators hex, oct, dec and setbase. #include <iostream> using using using using using using std::cout; std::cin; std::endl; std::hex; std::dec; std::oct;
34
Outline
fig12_08.cpp (1 of 2)
#include <iomanip> using std::setbase; int main() { int number; cout << "Enter a decimal number: "; cin >> number; // input number
// use hex stream manipulator to show hexadecimal number cout << number << " in hexadecimal is: " << hex << number << endl;
26 27 28 29 30 31 32 33 34 35 36 37
35
// use oct stream manipulator to show octal number cout << dec << number << " in octal is: " << oct << number << endl; // use setbase stream manipulator to show decimal number cout << setbase( 10 ) << number << " in decimal is: " << number << endl;
Outline
fig12_08.cpp (2 of 2) fig12_08.cpp output (1 of 1)
return 0;
} // end main a decimal number: 20 hexadecimal is: 14 octal is: 24 decimal is: 20
Enter 20 in 20 in 20 in
36
12.6.2 Floating-Point Precision (precision, setprecision) Set precision of floating point numbers
Number of digits to right of decimal setprecision stream manipulator
Pass number of decimal points cout << setprecision(5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.9: fig12_09.cpp // Controlling precision of floating-point values. #include <iostream> using using using using std::cout; std::cin; std::endl; std::fixed;
37
Outline
fig12_09.cpp (1 of 2)
#include <iomanip>
using std::setprecision; #include <cmath> // sqrt prototype
int main() { double root2 = sqrt( 2.0 ); // calculate square root of 2 int places; cout << "Square root of 2 with precisions 0-9." << endl Use fixed precision, not << "Precision set by ios_base member-function " scientific notation (more << "precision:" << endl;
details in 12.7).
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
38
// display square root using ios_base function precision for ( places = 0; places <= 9; places++ ) { cout.precision( places ); fig12_09.cpp cout << root2 << endl; Note format of function(2 of 2) } precision and cout << "\nPrecision set by stream-manipulator " << "setprecision:" << endl;
Outline
// set precision for each digit, then display square root for ( places = 0; places <= 9; places++ ) cout << setprecision( places ) << root2 << endl; return 0; } // end main
Square root of 2 with precisions 0-9. Precision set by ios_base member-function precision: 1 1.4 1.41 1.414 1.4142 1.41421 1.414214 1.4142136 1.41421356 1.414213562 Precision set by stream-manipulator setprecision: 1 1.4 1.41 1.414 1.4142 1.41421 1.414214 1.4142136 1.41421356 1.414213562
39
Outline
fig12_09.cpp output (1 of 1)
40
12.6.3 Field Width (width, setw) width member function (base class ios_base)
cin.width(5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Fig. 12.10: fig12_10.cpp // Demonstrating member function width. #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int widthValue = 4; char sentence[ 10 ]; cout << "Enter a sentence:" << endl; Reads up to 4 characters, cin.width( 5 ); // input only 5 characters from sentence
41
Outline
fig12_10.cpp (1 of 2)
// set field width, then display characters based on that width while ( cin >> sentence ) { Increment the output cout.width( widthValue++ ); cout << sentence << endl; cin.width( 5 ); // input 5 more characters from sentence } // end while return 0;
25 26
42
} // end main
Outline
fig12_10.cpp (2 of 2) fig12_10.cpp output (1 of 1)
Enter a sentence: This is a test of the width member function This is a test of the widt h memb er func tion
43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Fig. 12.11: fig12_11.cpp // Creating and testing programmer-defined, nonparameterized // stream manipulators. #include <iostream> using std::ostream; using std::cout; using std::flush;
44
Outline
fig12_11.cpp (1 of 3)
// bell manipulator (using escape sequence \a) ostream& bell( ostream& output ) { return output << '\a'; // issue system beep }
// carriageReturn manipulator (using escape sequence \r) ostream& carriageReturn( ostream& output ) { return output << '\r'; // issue carriage return }
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// tab manipulator (using escape sequence \t) ostream& tab( ostream& output ) { return output << '\t'; // issue tab } // endLine manipulator (using escape sequence \n and member // function flush) ostream& endLine( ostream& output ) { return output << '\n' << flush; // issue end of line } int main() { // use tab and endLine manipulators cout << "Testing the tab manipulator:" << endLine << 'a' << tab << 'b' << tab << 'c' << endLine; cout << "Testing the carriageReturn and bell manipulators:" << endLine << "..........";
45
Outline
fig12_11.cpp (2 of 3)
46 47 48 49 50 51
// use carriageReturn and endLine manipulators cout << carriageReturn << "-----" << endLine; return 0; } // end main
46
Outline
fig12_11.cpp (3 of 3) fig12_11.cpp output (1 of 1)
Testing the tab manipulator: a b c Testing the carriageReturn and bell manipulators: -----.....
47
12.7 Stream Format States and Stream Manipulators Many stream manipulators for formatting
Coming up next All inherit from ios_base
48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.13: fig12_13.cpp // Using showpoint to control the printing of // trailing zeros and decimal points for doubles. #include <iostream> using std::cout; using std::endl; using std::showpoint;
49
Outline
fig12_13.cpp (1 of 2)
int main() { // display double values with default stream format cout << "Before using showpoint" << endl << "9.9900 prints as: " << 9.9900 << endl << "9.9000 prints as: " << 9.9000 << endl << "9.0000 prints as: " << 9.0000 << endl << endl;
// display double value after showpoint cout << showpoint << "After using showpoint" << endl << "9.9900 prints as: " << 9.9900 << endl << "9.9000 prints as: " << 9.9000 << endl << "9.0000 prints as: " << 9.0000 << endl; return 0;
26 27
50
} // end main using showpoint prints as: 9.99 prints as: 9.9 prints as: 9
Outline
fig12_13.cpp (2 of 2) fig12_13.cpp output (1 of 1)
After using showpoint 9.9900 prints as: 9.99000 9.9000 prints as: 9.90000 9.0000 prints as: 9.00000
51
internal
Number's sign left-justified Number's value right-justified + 123 showpos forces sign to print
Remove with noshowpos
2003 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.14: fig12_14.cpp // Demonstrating left justification and right justification. #include <iostream> using using using using std::cout; std::endl; std::left; std::right;
52
Outline
fig12_14.cpp (1 of 2)
#include <iomanip>
using std::setw; int main() { int x = 12345;
// display x right justified (default) cout << "Default is right justified:" << endl << setw( 10 ) << x;
// use left manipulator to display x left justified cout << "\n\nUse std::left to left justify x:\n" << left << setw( 10 ) << x;
26 27 28 29 30 31 32
// use right manipulator to display x right justified cout << "\n\nUse std::right to right justify x:\n" << right << setw( 10 ) << x << endl; return 0; } // end main
53
Outline
fig12_14.cpp (2 of 2) fig12_14.cpp output (1 of 1)
Default is right justified: 12345 Use std::left to left justify x: 12345 Use std::right to right justify x: 12345
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Fig. 12.15: fig12_15.cpp // Printing an integer with internal spacing and plus sign. #include <iostream> using using using using std::cout; std::endl; std::internal; std::showpos;
54
Outline
fig12_15.cpp (1 of 1) fig12_15.cpp output (1 of 1)
#include <iomanip>
using std::setw; int main() showpos. { // display value with internal spacing and plus sign cout << internal << showpos << setw( 10 ) << 123 << endl; return 0; } // end main
123
55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Fig. 12.16: fig12_16.cpp // Using member-function fill and stream-manipulator setfill // to change the padding character for fields larger the // printed value. #include <iostream> using using using using using using using using std::cout; std::endl; std::showbase; std::left; std::right; std::internal; std::hex; std::dec;
56
Outline
fig12_16.cpp (1 of 3)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
int main() { int x = 10000; // display x cout << x << " printed as int right and left justified\n" << "and as hex with internal justification.\n" << "Using the default pad character (space):" << endl;
57
Outline
fig12_16.cpp (2 of 3)
// display x with plus sign cout << showbase << setw( 10 ) << x << endl;
// display x with left justification cout << left << setw( 10 ) << x << endl; // display x as hex with internal justification cout << internal << setw( 10 ) << hex << x << endl << endl; cout << "Using various padding characters:" << endl;
// display x using padded characters (right justification) cout << right; cout.fill( '*' ); cout << setw( 10 ) << dec << x << endl;
46 47 48 49 50 51 52 53 54 55
// display x using padded characters (left justification) cout << left << setw( 10 ) << setfill( '%' ) << x << endl; // display x using padded characters (internal justification) cout << internal << setw( 10 ) << setfill( '^' ) << hex << x << endl; return 0;
58
Outline
fig12_16.cpp (3 of 3) fig12_16.cpp output (1 of 1)
} // end main
10000 printed as int right and left justified and as hex with internal justification. Using the default pad character (space): 10000 10000 0x 2710 Using various padding characters: *****10000 10000%%%%% 0x^^^^2710
59
12.7.4 Integral Stream Base (dec, oct, hex, showbase) Print integer in various bases
dec, oct, hex
Stream extraction
Decimal numbers default Preceding 0 for octal Preceding 0x or 0X for hex
showbase
Forces base of number to be shown Remove with noshowbase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.17: fig12_17.cpp // Using stream-manipulator showbase. #include <iostream> using using using using using std::cout; std::endl; std::showbase; std::oct; std::hex;
60
Outline
fig12_17.cpp (1 of 1)
Forces numbers to be printed a preceding 0 (if octal) // use showbase to show numberwith base cout << "Printing integers preceded byhexadecimal). their base:" << endl or 0x (if
<< showbase; cout << x << endl; // print decimal value cout << oct << x << endl; // print octal value cout << hex << x << endl; // print hexadecimal value
return 0;
} // end main
61
Outline
fig12_17.cpp output (1 of 1)
62
12.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) Stream manipulator scientific
Forces scientific notation
1.946000e+009
If no manipulator specified
Format of number determines how it appears
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Fig. 12.18: fig12_18.cpp // Displaying floating-point values in system default, // scientific and fixed formats. #include <iostream> using using using using std::cout; std::endl; std::scientific; std::fixed;
63
Outline
fig12_18.cpp (1 of 2)
// display x and y in default format cout << "Displayed in default format:" << endl << x << '\t' << y << endl; // display x and y in scientific format cout << "\nDisplayed in scientific format:" << endl << scientific << x << '\t' << y << endl;
24 25 26 27 28 29 30
// display x and y in fixed format cout << "\nDisplayed in fixed format:" << endl << fixed << x << '\t' << y << endl; return 0; } // end main
64
Outline
fig12_18.cpp (2 of 2)
Displayed in default format: 0.00123457 1.946e+009 Displayed in scientific format: 1.234567e-003 1.946000e+009 Displayed in fixed format: 0.001235 1946000000.000000
fig12_18.cpp output (1 of 1)
65
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 12.19: fig12_19.cpp // Stream-manipulator uppercase. #include <iostream> using using using using std::cout; std::endl; std::uppercase; std::hex;
66
Outline
fig12_19.cpp output (1 of 1)
int main() { cout << "Printing uppercase letters in scientific" << endl << "notation exponents and hexadecimal values:" << endl;
Printing uppercase letters in scientific notation exponents and hexadecimal values: 4.345E+010 75BCD15
67
Outline
fig12_19.cpp output (1 of 1)
68
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.20: fig12_20.cpp // Demonstrating stream-manipulators boolalpha and noboolalpha. #include <iostream> using using using using using std::cout; std::endl; std::cin; std::boolalpha; std::noboolalpha;
69
Outline
fig12_20.cpp (1 of 2)
bool variables can be false or true. Note use of the boolalpha stream manipulator.
// display default true booleanValue cout << "booleanValue is " << booleanValue << endl; // display booleanValue after using boolalpha cout << "booleanValue (after using boolalpha) is " << boolalpha << booleanValue << endl << endl; cout << "switch booleanValue and use noboolalpha" << endl; booleanValue = false; // change booleanValue cout << noboolalpha << endl; // use noboolalpha
26 27 28 29 30 31 32 33 34 35
// display default false booleanValue after using noboolalpha cout << "booleanValue is " << booleanValue << endl; // display booleanValue after using boolalpha again cout << "booleanValue (after using boolalpha) is " << boolalpha << booleanValue << endl; return 0;
70
Outline
fig12_20.cpp (2 of 2) fig12_20.cpp output (1 of 1)
} // end main
booleanValue is 1 booleanValue (after using boolalpha) is true switch booleanValue and use noboolalpha booleanValue is 0 booleanValue (after using boolalpha) is false
71
12.7.8 Setting and Resetting the Format State via Member-Function flags Can save/restore format states
After apply many changes, may want to restore original
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Fig. 12.21: fig12_21.cpp // Demonstrating the flags member function. #include <iostream> using using using using using using std::cout; std::endl; std::oct; std::scientific; std::showbase; std::ios_base;
72
Outline
fig12_21.cpp (1 of 2)
int main() { int integerValue = 1000; double doubleValue = 0.0947628; // display flags value, int and cout << "The value of the flags << "\nPrint int and double << integerValue << '\t' << double values (original format) variable is: " << cout.flags() in original format:\n" Save original doubleValue << endl << endl;
// use cout flags function to save original format ios_base::fmtflags originalFormat = cout.flags(); cout << showbase << oct << scientific; // change format
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// display flags value, int and cout << "The value of the flags << "\nPrint int and double << integerValue << '\t' <<
double values (new format) variable is: " << cout.flags() in a new format:\n" doubleValue << endl << endl;
73
Outline
fig12_21.cpp (2 of 2)
cout.flags( originalFormat ); // restore format // display flags value, int and double values (original format) cout << "The restored value of the flags variable is: " << cout.flags() << "\nPrint values in original format again:\n" << integerValue << '\t' << doubleValue << endl; return 0; } // end main
The value of the flags variable is: 513 Print int and double in original format: 1000 0.0947628 The value of the flags variable is: 012011 Print int and double in a new format: 01750 9.476280e-002 The restored value of the flags variable is: 513 Print values in original format again: 1000 0.0947628
74
Outline
fig12_21.cpp output (1 of 1)
75
76
clear()
Default argument goodbit Sets stream to "good" state, so I/O can continue Can pass other values cin.clear( ios::failbit ) Sets failbit Name "clear" seems strange, but correct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Fig. 12.22: fig12_22.cpp // Testing error states. #include <iostream> using std::cout; using std::endl; using std::cin; int main() { int integerValue;
77
Outline
fig12_22.cpp (1 of 2)
// display results of cin functions cout << "Before a bad input operation:" << "\ncin.rdstate(): " << cin.rdstate() << "\n cin.eof(): " << cin.eof() << "\n cin.fail(): " << cin.fail() << "\n cin.bad(): " << cin.bad() << "\n cin.good(): " << cin.good() << "\n\nExpects an integer, but enter a character: "; cin >> integerValue; // enter character value cout << endl;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// display results of cin functions after bad input cout << "After a bad input operation:" << "\ncin.rdstate(): " << cin.rdstate() << "\n cin.eof(): " << cin.eof() << "\n cin.fail(): " << cin.fail() Note the use of clear. << "\n cin.bad(): " << cin.bad() << "\n cin.good(): " << cin.good() << endl << endl; cin.clear(); // clear stream // display results of cin functions after clearing cin cout << "After cin.clear()" << "\ncin.fail(): " << cin.fail() << "\ncin.good(): " << cin.good() << endl; return 0; } // end main
78
Outline
fig12_22.cpp (2 of 2)
Before a bad input operation: cin.rdstate(): 0 cin.eof(): 0 cin.fail(): 0 cin.bad(): 0 cin.good(): 1 Expects an integer, but enter a character: A
79
Outline
fig12_22.cpp output (1 of 1)
80
12.9 Tying an Output Stream to an Input Stream Problem with buffered output
Interactive program (prompt user, he/she responds) Prompt needs to appear before input proceeds
Buffered outputs only appear when buffer fills or flushed