Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Input output.

Similar presentations


Presentation on theme: "Chapter 3 Input output."— Presentation transcript:

1 Chapter 3 Input output

2 Objectives In this chapter, you will:
Discover how to input data into memory using input statements Explore how to read data from the standard input device Learn how to debug a program using cout statements Learn how to debug a program by understanding error messages

3 Objectives (continued)
Explore how to use the input stream functions get, ignore, and clear Become familiar with input failure Learn how to perform input and output operations with the string data type Learn how to format output using the manipulators fixed, showpoint, setprecision, setw, left, and right Become familiar with file input and output

4 Input (Read) Statement
The syntax of an input (read) statement using cin and the stream extraction operator >> is: During programming execution, if more than one value is entered in a line, these values must be separated by at least one blank or tab Or, one value per line can be entered

5 Input (Read) Statement (continued)
Must be included to use cin So we can refer to cin without using std:: 5

6 Variable Initialization
C++ does not automatically initialize variables when they are declared Ways to initialize a variable By using the assignment statement feet = 35; By using a read statement cin >> feet; More versatile 6

7 7

8 Programming Style and Form (Revisited)
In Chapter 1, you learned about syntax, semantics, errors, and how to format a program properly In this section, we extend our capabilities to include prompt lines in a program 8

9 Prompt Lines Prompt lines: executable statements that tell the user what to do to interact with the program cout << "Please enter a number between 1 and 10 and then " << " press Enter." << endl; cin >> num; Prompt lines should include sufficient information about what input is acceptable 9

10 Debugging – Sprinkling with couts
Walk-throughs may fail to reveal bugs Inserting temporary strategically located cout statements can aid in the debugging process int fahrenheit = 32; int celsius = 5 / 9 * (fahrenheit - 32); cout << fahrenheit << " degrees " << "Fahrenheit is " << celsius << " degrees " << "Celsius. " << endl; cout << "5 / 9 is " << 5 / 9 << " and fahrenheit - 32 is " << fahrenheit - 32 << endl; 10

11 Debugging – Understanding Error Messages
The C++ compiler will find syntactic errors in your program and will provide messages describing the errors Provide a good indication of where to look for the problem(s) Look carefully in the immediate vicinity of the reported error Finding and reporting errors is like peeling away layers of an onion 11

12 More on Input >> distinguishes between character 2 and number 2 by the right-side operand of >> Entering a char value into an int or double variable causes serious errors; called input failure 12

13 More on Input (continued)
When reading data into a char variable: >> skips leading whitespace; finds and stores only the next character Reading stops after a single character To read data into an int or double variable: >> skips leading whitespace; reads + or - sign (if any); reads the digits (including decimal) Reading stops on whitespace non-digit character

14 More on Input (continued)

15 More on Input (continued)
15

16 cin and the get Function
Inputs next character (including whitespace) Stores in memory location indicated by its argument The syntax of cin and the get function is: where varChar : Is a char variable Is the argument (parameter) of the function

17 cin and the ignore Function
ignore: discards a portion of the input The syntax to use the function ignore is: intExp is an integer expression chExp is a char expression If intExp is a value m, the statement says to ignore the next m characters or all characters until the character specified by chExp

18 cin and the ignore Function (continued)
18

19 cin and the ignore Function (continued)
19

20 Input Failure Things can go wrong during execution
If input data does not match corresponding variables, program may run into problems Trying to read a letter into an int or double variable will result in an input failure If an error occurs when reading data: Input stream enters the fail state

21 Input Failure (continued)
21

22 Input Failure (continued)
Sample run 1 Line 9: Enter four integers: 34 K 67 28 Line 12: The numbers you entered are: Line 13: a = 34, b = 20, c = 30, d = 40 Sample run 2 Line 9: Enter four integers: Line 13: a = 37, b = 653, c = 30, d = 40 22

23 The clear Function Once in a fail state, all further I/O statements using that stream are ignored The program continues to execute with whatever values are stored in variables This causes incorrect results The clear function restores input stream to a working state

24 24

25 The clear Function (continued)
Sample run Line 7: Enter a number followed by a character: 78 d Line 9: a = 78, b = 34 Line 12: Enter two numbers: 65 88 Line 14: a = 65, b = 88 25

26 Input/Output and the string Type
An input stream variable (cin) and >> operator can read a string into a variable of the data type string Extraction operator Skips any leading whitespace characters and reading stops at a whitespace character The function getline Reads until end of the current line 26

27 Formatting Output Other than writing efficient programs, generating the desired output is one of a programmer’s highest priorities In this section, you will learn about various output functions and manipulators that allow you to format your output in a desired way

28 setprecision Manipulator
Syntax is: Outputs decimal numbers with up to n decimal places Must include the header file iomanip: #include <iomanip>

29 fixed Manipulator fixed outputs floating-point numbers in a fixed decimal format Disable by using the stream member function unsetf The manipulator scientific is used to output floating-point numbers in scientific format

30 showpoint Manipulator
showpoint forces output to show the decimal point and trailing zeros Example cout << fixed << showpoint;

31 31

32 showpoint Manipulator (continued)
Sample run Line 10: setprecision(2) Line 11: x = 15.67 Line 12: y = Line 13: z = Line 14: setprecision(3) Line 15: x = Line 16: y = Line 17: z = Line 18: setprecision(4) Line 19: x = Line 20: y = Line 21: z = 32

33 setw Outputs the value of an expression in specific columns
cout << setw(5) << x << endl; If number of columns exceeds the number of columns required by the expression: Output of the expression is right-justified Unused columns to the left are filled with spaces Must include the header file iomanip

34 34

35 setw (continued) Sample run 12345678901234567890 19 345 Hi 19
34519 35

36 left and right Manipulators
left: left-justifies the output right: right-justifies the output

37 left and right Manipulators (continued)
37

38 left and right Manipulators (continued)
Sample run Warm Warm 38

39 File Input/Output File: area in secondary storage to hold info
File I/O is a five-step process Include fstream header Declare file stream variables Associate the file stream variables with the input/output sources (requires opening the files) Use the file stream variables with >>, <<, or other input/output functions Close the files File Input/Output

40 File Input/Output (continued)
40

41 Programming Example: Student Grade
Write a program that reads a student name followed by five test scores The program should output the student name, the five test scores, and the average test score Output the average test score with two decimal places The data to be read is stored in a file called test.txt The output should be stored in a file called testavg.out

42 Programming Example: I/O
Input A file containing the student name and the five test scores Example: Andrew Miller Output The student name, the five test scores, and the average of the five test scores, saved to a file

43 Programming Example: Problem Analysis
The program needs to: Read the student name and the five test scores Output the student name and the five test scores Calculate the average Output the average

44 Programming Example: Variables
ifstream inFile; ofstream outFile; double test1, test2, test3, test4, test5; double average; string firstName; string lastName;

45 Programming Example: Main Algorithm
Declare the variables Open the input file Open the output file Set the manipulators fixed and showpoint Also, set the precision to two decimal places Read the student name

46 Main Algorithm (continued)
Output the student name Read the five test scores Output the five test scores Find the average test score Output the average test score Close the input and output files

47 Summary Input from the standard input device is accomplished by using cin and >> >> is the stream extraction operator When data is input in a program, the data items are usually separated by blanks, lines, or tabs When inputting data into a variable, >> skips all leading whitespace characters To use cin, include the header file iostream Prompt lines: executable statements that tell user what to do

48 Summary (continued) get reads data character by character
ignore skips data in a line clear is used to restore the input stream (from input failure) to a working state After attempting to read invalid data into a variable setprecision, fixed, showpoint, setw, left, and right can be used to format output Include iomanip to use setprecision and setw Header fstream contains the definitions of ifstream and ofstream


Download ppt "Chapter 3 Input output."

Similar presentations


Ads by Google