CHAP04 - Chapter 4 Program Input and the Software Design...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Chapter 4 Topics Input Statements to Read Values for a Program using >> , and functions get , ignore , getline Prompting for Interactive Input/Output Using Data Files for Input and Output Object-Oriented Design Principles Functional Decomposition Methodology
Background image of page 2
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
<iostream> is header file for a library that defines 3 objects an istream object named cin (keyboard) an ostream object named cout (screen) an ostream object named cerr (screen)
Background image of page 4
5 Giving a Value to a Variable In your program you can assign (give) a value to the variable by using the assignment operator = ageOfDog = 12; or by another method, such as cout << “How old is your dog?”; cin >> ageOfDog;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
>> is a binary operator >> is called the input or extraction operator >> is left associative EXPRESSION HAS VALUE cin >> age cin STATEMENT cin >> age >> weight ;
Background image of page 6
Extraction Operator ( >> ) variable cin is predefined to denote an input stream from the standard input device ( the keyboard ) the extraction operator >> called “get from” takes 2 operands. The left operand is a stream expression, such as cin--the right operand is a variable of simple type. operator >> attempts to extract the next item from the input stream and store its value in the right operand variable
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
SYNTAX These examples yield the same result. cin >> length ; cin >> width ; cin >> length >> width ; Input Statements cin >> Variable >> Variable . . . ;
Background image of page 8
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.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Extraction Operator >> “skips over” (actually reads but does not store anywhere ) leading white space characters as it reads your data from the input stream (either keyboard or disk file)
Background image of page 10
char first ; char middle ; char last ; cin >> first ; cin >> middle ; cin >> last ; NOTE: A file reading marker is left pointing to the newline character after the ‘C’ in the input stream. first middle last At keyboard you type: A [ space]B [ space ] C [ Enter] first middle last ‘A’ ‘B’ ‘C’
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
At keyboard you type: [ space ] 25 [ space ] J [ space]2 [ Enter ] int age ; char initial ; float bill ; cin >> age ; cin >> initial ; cin >> bill ; NOTE: A file reading marker is left pointing to the newline character after the 2 in the input stream. age initial bill age initial bill 25 ‘J’ 2.0
Background image of page 12
Keyboard and Screen I/O #include <iostream> cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data output data
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CONTENTS MARKER POSITION int i ; 25 A\n char ch ; 16.9\n float x ; cin >> i ; 25 A\n 16.9\n cin >> ch ; 25 A \n 16.9\n cin >> x ; 25 A\n 16.9 \n Another example using >>
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 57

CHAP04 - Chapter 4 Program Input and the Software Design...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online