1 / 19

Chapter 4 Program Input and the Software Design Process

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

elwyn
Download Presentation

Chapter 4 Program Input and the Software Design Process

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. Chapter 4Program Input and the Software Design Process Dale/Weems

  2. Input Statements to Read Values into a Program using >> • Prompting for Interactive Input/Output

  3. 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

  4. 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.

  5. 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

  6. <iostream> Header File Access to a library that defines 2 objects • An istream object named cin (keyboard) • An ostream object named cout (screen) 6

  7. 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;

  8. >> 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

  9. Input Statements SYNTAX These examples yield the same result. cin >> length; cin >> width; cin >> length >> width; cin >> Variable >> Variable .. .; 10

  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

  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

  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

  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

  14. String Input in C++ Input of a string is possible using the extraction operator >> Example string message; cin >> message; cout << message; However . . .

  15. >> 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)

  16. String Input Using >> string firstName; string lastName; cin >> firstName >> lastName; Suppose input stream looks like this: JoeHernandez 23 What are the string values?

  17. Results Using >> string firstName; string lastName; cin >> firstName >> lastName; Result “Joe” “Hernandez” firstName lastName

  18. 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

  19. Prompting for Interactive I/O // DO: prompt the user to enter three integers and print out their average 32

More Related