Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.

Similar presentations


Presentation on theme: "1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What."— Presentation transcript:

1 1 Program Input Software Design Chapter 4

2 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What is object oriented design?

3 3 Getting Data Into Programs n A program is more versatile when it can be used with different sets of data n This requires keeping data separate from the program until execution time n Input placing values from outside the program into the running program Program to print paychecks Employee data Timecard data Paychecks Payroll register

4 4 n Input data is considered to be an endless sequence of characters/bytes coming into a program from an input device (keyboard, file, etc.) Input Streams … 1 4 19 34 HI MOM. 7..

5 5 Input Streams n Preprocessor directive sets things up for standard I/O (keyboard and screen) n This provides the programmer with data types and commands for accomplishing I/O

6 6 The Extraction Operator >> Enables you to do input with the cin command Enables you to do input with the cin command n Think of the >> as pointing to where the data will end up n C++ able to handle different types of data and multiple inputs correctly

7 7 The Reading Marker n Keeps track of point in the input stream where the computer should continue reading n Extraction >> operator leaves reading marker following last piece of data read … 1 4 19 34 HI MOM. 7..

8 8 The Reading Marker During execution of a cin command During execution of a cin command –as long as it keeps finding data, it keeps reading –when the reading marker hits something not data, it quits reading Things in the input stream that cin considers not data Things in the input stream that cin considers not data –spaces –tab \t –newline character \n (pressing the RETURN key) –for numeric input, something nonnumeric

9 9 Reading String Data with cin n Keyboard response of two words (separated by a space) causes the cin command to quit reading –the space is considered nondata (in spite of our intent) ???

10 10 Reading String Data The getline ( ) function will allow the programmer to access all the characters The getline ( ) function will allow the programmer to access all the characters String variable Length (max number of characters) Character which terminates read

11 11 Using the getline( ) n Problem : the getline( ) quits reading when it finds a newline –Suppose you have terminated previous input with the key (newline still in input stream) –getline ( ) finds the newline immediately and declares its task finished –we must somehow discard the newline in the input stream ???

12 12 Using the ignore( ) Solution : the ignore( ) command Solution : the ignore( ) command n Tells the program to skip either the next 10 characters or until it reaches a newline –whichever comes first n This effectively discards the newline

13 13 Interactive I/O n Defn -> The user communicates directly with the computer The programmer (you) should always use “input prompts” to tell the user what is expected Enter amt :

14 14 Interactive I/O n Using input prompts -- combination of … –cout for the prompt –cin for the input

15 15 Noninteractive I/O n The user does not communicate directly with the computer n The data comes from a file (batch processing) n The output goes to a file or the printer n Prompting does not usually occur --Often messages are sent to the screen to inform the user as to what is happening Program to print paychecks Employee data Timecard data Paychecks Payroll register

16 16 File Input and Output n File named area in secondary memory –holds collection of data/information n Examples –source code of a program is the “data” for the compiler –time card files for payroll n Most useful for large amounts of data

17 17 Using Files in Programs n Specify #include header file n Declare instance of file to be used Prepare for access with.open( ) command Prepare for access with.open( ) command n Use name of file in place of cin or cout file name on disk

18 18 Input Failure n Things can go wrong –invalid data from keyboard –wrong file name entered in n Problem: C++ does not tell us something is wrong -- it goes merrily on its way n Echo print incoming data to verify input values are correct n Test program to make sure it is giving reasonable results

19 19 Software Design n Our 10 - 20 line lab programs and class examples are fairly simple to understand, grasp, plan out n A more systematic approach for software design is needed for larger programs n Methodologies used –Top-down design –Object oriented design

20 20 Top Down Design n Defn => a technique for developing a program –Problem divided into more easily handled sub- tasks –Solutions to sub-tasks create the solution to the overall problem n Consider the following task

21 21 Top Down Design

22 22 Testing and Debugging Hints n I/O statements always begin with the name of a stream variable n > operators point in the direction in which data is going n Make sure.open( ) statement has correct name of file on the disk n If reading from or writing to a file, make sure to use the name of the file/stream not cin or cout

23 23 Testing and Debugging Hints n Make sure input statements have the correct number of variables n Make sure input statements have the correct variable types n If input data is mixed string and numeric, make sure to deal with the blanks and newlines correctly n Echo print input data to verify each value where it belongs and is in proper format


Download ppt "1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What."

Similar presentations


Ads by Google