190 likes | 352 Views
Chapter 4 Program Input and the Software Design Process. Dale/Weems. Input Statements to Read Values into a Program using >> Prompting for Interactive Input/Output. C++ Input/Output. No built-in I/O in C++!! A library provides input stream and output stream. Keyboard. Screen. executing
E N D
Chapter 4Program Input and the Software Design Process Dale/Weems
Input Statements to Read Values into a Program using >> • Prompting for Interactive Input/Output
C++ Input/Output • No built-in I/O in C++!! • A library provides input stream and output stream Keyboard Screen executing program istream ostream 3
Using Libraries • A library has 2 parts Interface(stored in a header file)tells what items are in the library and how to use them Implementation(stored in another file)contains the definitions of the items in the library • #include <iostream> Refers to the header file for the iostream library needed for use of cout and endl.
Is compilation the first step? • No; before your source program is compiled, it is first examined by the preprocessorthat • removes all comments from source code • handles all preprocessor directives--they begin with the # character such as #include <iostream> • This include tells the preprocessor to look in the standard include directory for the header file called iostream and insert its contents into your source code
<iostream> Header File Access to a library that defines 2 objects • An istream object named cin (keyboard) • An ostream object named cout (screen) 6
Giving a Value to a Variable RECALL: 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;
>> Operator >> is called the input or extraction operator >> is a binary operator >> is left associative Expression Has value cin >> age cin Statement cin >> age >> weight; 8
Input Statements SYNTAX These examples yield the same result. cin >> length; cin >> width; cin >> length >> width; cin >> Variable >> Variable .. .; 10
Extraction Operator >> >> “skips over”(actually reads but does not store anywhere) leading white space characters as it reads your data from the input stream 11
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 12
char first; char middle; char last; cin >> first ; cin >> middle ; cin >> last ; NOTE: A reading marker is left pointing to the newline character after the ‘C’ in the input stream At the keyboard you type:A[space]B[space]C[Enter] first middle last ‘A’ ‘B’ ‘C’ first middle last 13
i ch x i ch x i ch x i ch x STATEMENTS CONTENTS MARKER POSITION int i; char ch; float x; cin >> i; cin >> ch; cin >> x; Another example using >> 25 A\n 16.9\n 25 A\n 16.9\n 25 A\n 16.9\n 25 A\n 16.9\n 25 25 ‘A’ ‘A’ 25 16.9 16
String Input in C++ Input of a string is possible using the extraction operator >> Example string message; cin >> message; cout << message; However . . .
>> Operator with Strings • The >> operator skips any leading whitespace characters such as blanks and newlines • It then reads successive characters into the string, and stops at the first trailing whitespace character(which is not consumed, but remains waiting in the input stream)
String Input Using >> string firstName; string lastName; cin >> firstName >> lastName; Suppose input stream looks like this: JoeHernandez 23 What are the string values?
Results Using >> string firstName; string lastName; cin >> firstName >> lastName; Result “Joe” “Hernandez” firstName lastName
Interactive I/O • In an interactive program the user enters information while the program is executing • Before the user enters data, a prompt should be provided to explain what type of information should be entered • The amount of information needed in the prompt depends on • the complexity of the data being entered, and • the sophistication of the person entering the data 31
Prompting for Interactive I/O // DO: prompt the user to enter three integers and print out their average 32