1 / 16

Input/Output

Input/Output. Disk Drive. Disk Drive. istream. ostream. Main Memory. Monitor. Keyboard. Printer. stream = sequence of bytes. Scanner. Input/Output standard library. Unformatted – stream of bytes. Good for high speed, high volume transfers

bevan
Download Presentation

Input/Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Input/Output Disk Drive Disk Drive istream ostream Main Memory Monitor Keyboard Printer stream = sequence of bytes Scanner

  2. Input/Output standard library • Unformatted – stream of bytes. Good for high speed, high volume transfers • Formatted – groupings of bytes. Integers, characters, etc.

  3. iostream library - cin, cout, cerr • an istream object named cin associated with the keyboard • an ostream object named cout associated with the monitor • an ostream object named cerr associated with the monitor

  4. operators • << stream insertion operator int grade = 5; cout << grade; • >> stream extraction operator int grade; cin >> grade;

  5. cout details int grade = 92; cout << grade; // causes 92 to be displayed on the screen integer char 92 grade 2 9 4 bytes 2 bytes • steps: • convert integer 92 to character sequence 92 • send characters 9 2 to the screen

  6. cout details cout << ‘9’ << ‘2’; // causes 92 to be displayed on the screen char char 2 2 9 9 2 bytes 2 bytes no conversion necessary cout knows what to do because it knows the data type

  7. concatenation of stream operators int grade = 92; cout << “your grade is” << grade; is the same as: cout << “your grade is”; cout << grade;

  8. cout escape sequences • \ escape character. special character follows cout << “Hello \n”; \n newline \\ backslash \” double quote \t tab

  9. stream manipulators • endl – writes a newline, flushes buffer cout << “your grade is” << grade << endl; • what’s a buffer? place to hold data while waiting for I/O operation to complete. i/o device takes things out cout puts things in 2 9 first in first out buffer • 1 second = 1000 milliseconds = 1 million microseconds • cout << grade; // microseconds to execute • // milliseconds or seconds to complete I/O

  10. stream manipulators double veryPreciseNumber = 1.123456789; cout << veryPreciseNumber << endl; prints 1.12346 cout << setprecision(10) << veryPreciseNumber; prints 1.123456789 #include <iomanip> //needed to use setprecision stays in effect for all subsequent cout calls setprecision(0) sets it back to default value

  11. stream manipulators cout.setf(ios::showpoint); • ios::hex   Format numeric values as base 16 (hexadecimal). • ios::showpoint   Show decimal point and trailing zeros for floating-point values. • ios::showpos   Show plus signs (+) for positive values. • ios::scientific   Display floating-point numbers in scientific format. • ios::fixed   Display floating-point numbers in fixed format.

  12. cout Exercises Identify the error in each of the following statements and explain how to correct it. 1) The following statement will print the integer value for ‘c’. cout << ‘c’; 2) cout << “”A string in quotes”” << endl; 3) display the number 200 with and without the + sign.

  13. cin int age; cin >> age; • operand to right of >> must be a variable • nothing read until Enter key is typed • reads input until end of input value is reached. 12 (age = 12) 1 2 (age = 1) • input data must match data type. x would be an error for input for age.

  14. cin • concatenation okay int age, weight; cin >> age >> weight; Input: 25 150 • cin statement causes program to wait until input is entered. Should precede it with prompt cout << “Please enter age and weight”; • handout on cin in Prof Hamel’s section of web site • C++ How to program, Harvey Deitel Chapter 11

  15. cin.get Useful when you want to be read whitespace characters i.e write a program to change a single spaced document into a double spaced document. char nextCharacter; cin.get (nextCharacter);

  16. cin Exercises For each question, assume the following declarations: int num1, num2, num3; double real1, real2, real3; Each question is separate. 1) cin >> num1 >> num2 >> num3; INPUT 11 22 33 44 2) cin >> real1 >> real2 >> real3; INPUT 1.1 2 3.3 4 3) cin >> num1 >> num2 >> num3; INPUT 1.1 2 3.3 4 4) cin >> num1 >> real1 >> num2 >> real2 >> num3 >> real3; INPUT 1.1 2 3.3 4 5.5 6

More Related