Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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 2
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
Input (Read) Statement (continued) Must be included to use cin So we can refer to cin without using std:: 55
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 66
Creating a C++ Program (Revisited) In skeleton form, a C++ program looks like this: preprocessor directives to include header files using statement declare named constants, if necessary int main() { variable declaration statements return 0; } 77
88
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 99
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 10
Debugging – Sprinkling with cout s 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; 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 12
13 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
Input Failure (continued) 14
Input Failure (continued) Sample run 1 Line 9: Enter four integers: 34 K 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 12: The numbers you entered are: Line 13: a = 37, b = 653, c = 30, d = 40 15
16 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
17 setprecision Manipulator Syntax is: Outputs decimal numbers with up to n decimal places Must include the header file iomanip : #include
18 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
19 showpoint Manipulator showpoint forces output to show the decimal point and trailing zeros Example cout << fixed << showpoint;
20
showpoint Manipulator (continued) Sample run Line 10: setprecision(2) Line 11: x = 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 =
22 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
23
setw (continued) Sample run Hi
25 left and right Manipulators left : left-justifies the output right : right-justifies the output
left and right Manipulators (continued) 26
left and right Manipulators (continued) Sample run Warm 27
28 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
29 Summary (continued) setprecision, fixed, showpoint, setw, left, and right can be used to format output Include iomanip to use setprecision and setw