Chapter 4 Program Input and the Software Design Process Dale/Weems 1 Input Statements to Read Values into a Program using >> Prompting for Interactive Input/Output 2 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. 4 Is compilation the first step? No; before your source program is compiled, it is first examined by the preprocessor that 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 5 <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; 7 >> Operator >> is called the input or extraction operator >> is a binary operator >> is left associative Expression Has value cin cin >> age Statement cin >> age >> weight; 8 Input Statements SYNTAX cin >> Variable >> Variable . . .; These examples yield the same result. cin >> length; cin >> width; cin >> length >> width; 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 At the keyboard you type: A[space]B[space]C[Enter] char char char cin cin cin first; middle; last; >> >> >> first first ; middle ‘A’ ; last ; first middle last ‘B’ ‘C’ middle last NOTE: A reading marker is left pointing to the newline character after the ‘C’ in the input stream 13 Another example using >> STATEMENTS int char float i; ch; x; cin >> i; cin >> ch; cin >> x; CONTENTS MARKER POSITION 25 A\n 16.9\n i ch x 25 A\n 16.9\n 25 i ch 25 ‘A’ i ch 25 ‘A’ i ch x 25 A\n 16.9\n x 16.9 25 A\n 16.9\n x 16 String Input in C++ Input of a string is possible using the extraction operator >> Example string cin >> cout << message; message; message; However . . . 22 >> 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) 23 String Input Using >> string string cin >> firstName; lastName; firstName >> lastName; Suppose input stream looks like this: Joe Hernandez 23 What are the string values? 24 Results Using >> string string cin >> firstName; lastName; firstName >> lastName; Result “Joe” “Hernandez” firstName lastName 25 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