130 likes | 282 Views
Input/Output. Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics. I/O - Input/Output is one of the first aspects of programming that needs to be mastered: formatting whitespace structured inputs - getting data into the correct internal form However, do not fall into the beginner’s traps:
E N D
Input/Output Sujana Jyothi C++ Workshop Day 2
C++ I/O Basics • I/O - Input/Output is one of the first aspects of programming that needs to be mastered: • formatting • whitespace • structured inputs - getting data into the correct internal form • However, do not fall into the beginner’s traps: • brilliant I/O with incorrect functionality • thinking functionality is wrong because I/O is too complicated to get right • forgetting that random tests are often better than user dependent I/O 2
Scientific Notation #include <iostream> #include <iomanip> Using namespace std; main() { float z = 123456789.12335; cout << z << endl; } output may be: 1.23457e+08
Outline Simple input/output (iostream) cin and cout output insertion operator (<<) extraction operator (>>) Advanced input/output object flags (setf, unsetf) input status bits manipulators (iomanip.h) file input/output (fstream.h) opening/closing files
Setting Format Flags • The object cout has flags that determine how objects are printed. To change how things are printed we access and change these flags • To set a flag(s) we use the setf() function which is associated with objects such as cout and cin • To call setf() we say cout.setf(flags) • the setf function is a field of the object cout • Q: But what flags? A: C++ predefines them
Setting Format Flags (cont) • But in order to be able to set flags we often have to unset other flags first, to do so we use the unsetf() function: cout.unsetf(flags) • C++ also provides a short-hand to combine both operations: cout.setf(OnFlags,OffFlags) • First turns off the flags OffFlags • Then turns on the flags OnFlags
Explicit precision manipulation // Example 10 #include <iomanip> #include <iostream> Using namespace std; int main ( void){ float myNumber = 123.4587 ; cout.setf ( ios::fixed , ios::floatfield );// decimal format cout.setf ( ios::showpoint ) ; // print decimal point //ios is c++ standard library reference cout << "Number is " << setprecision ( 3 ) << myNumber << endl ; return 0 ; } Output: Number is 123.459 7
setw(n) requires #include <iomanip> and appears in an expression using insertion operator (<<) affects only the very next item displayed “set width” specifies n as the number of total columns to display a number. The number of columns used is expanded if n is too narrow. Useful to align columns of output
//Example 11 #include <iomanip> #include <iostream> Using namespace std; int main ( void) { float myNumber = 123.4 ; float yourNumber = 3.14159; cout.setf ( ios::fixed , ios::floatfield ) ; cout.setf ( ios::showpoint ) ; cout << "Numbers are: " << setprecision (4) << endl << setw ( 10 ) << myNumber << endl << setw ( 10 ) << yourNumber << endl ; return 0 ; } Setw() Example Numbers are: 123.4000 3.1416 Output: 9
Whitespace Characters Include . . . • blanks • tabs • end-of-line (newline) characters The newline character is created by hitting Enter or Return at the keyboard, or by using the manipulator endl or “\n” in a program.
Another way to read char data The get( ) function can be used to read a single character. It obtains the very next character from the input stream without skipping any leading white space characters. 11
At keyboard you type:A[space]B[space]C[Enter] char first ; char middle ; char last ; cin.get ( first ) ; cin.get ( middle ) ; cin.get ( last ) ; NOTE: The file reading marker is left pointing to the space after the ‘B’ in the input stream. first middle last ‘A’ ‘ ’ ‘B’ first middle last 12
Example program – I/O Formatting #include <iostream> #include <iomanip> int main() { int n; float f; double d; char s[100]; cin >> n; // input an integer cout << n << endl; // print an integer, no formatting cout << setw(6) << n << endl; // print an integer, padded on left with spaces to total 6 chars cout << setw(-6) << n << endl; // print an integer, padded on right with spaces to total 6 chars cin >> s; // input a string (whitespace delineated) cout << s << endl; // print a string, no formatting cout << setw(20) << s << endl; // print a string, padded with spaces on left to 20 chars cout << setiosflags(ios::left) << setw(20) << s << endl; // print a string, padded with spaces on right to 20 chars cin >> f; // input a single precision floating point number cout << setiosflags(ios::fixed) << f << endl; // print a float, default precision is 6 places cin >> d; // input a double precision floating point number cout << d << endl; // print a double, default precision is 6 places cout << setprecision(2) << d << endl; // print a double, 2 places of precision cout << setw(10) << setprecision(2) << d << endl; // print a double, 2 places of precision, padded with space to 10 }