Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT 1033 1.

Similar presentations


Presentation on theme: "Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT 1033 1."— Presentation transcript:

1 Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT 1033 1

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 2

3 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 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:: 55

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 66

7 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

8 88

9 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

10 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

11 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

12 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 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

14 Input Failure (continued) 14

15 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: 37 653.89 23 76 Line 12: The numbers you entered are: Line 13: a = 37, b = 653, c = 30, d = 40 15

16 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 17 setprecision Manipulator Syntax is: Outputs decimal numbers with up to n decimal places Must include the header file iomanip : #include

18 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 19 showpoint Manipulator showpoint forces output to show the decimal point and trailing zeros Example cout << fixed << showpoint;

20 20

21 showpoint Manipulator (continued) Sample run Line 10: setprecision(2) Line 11: x = 15.67 Line 12: y = 235.73 Line 13: z = 9525.99 Line 14: setprecision(3) Line 15: x = 15.674 Line 16: y = 235.730 Line 17: z = 9525.986 Line 18: setprecision(4) Line 19: x = 15.6740 Line 20: y = 235.7300 Line 21: z = 9525.9864 21

22 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 23

24 setw (continued) Sample run 12345678901234567890 19 345 Hi 19 345 76.38 19 19 345 76.38 34519 24

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

26 left and right Manipulators (continued) 26

27 left and right Manipulators (continued) Sample run 12345678901234567890 15 7634 Warm 27

28 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 29 Summary (continued) setprecision, fixed, showpoint, setw, left, and right can be used to format output Include iomanip to use setprecision and setw


Download ppt "Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT 1033 1."

Similar presentations


Ads by Google