operating system using setw and setprecision functions using setiosflags function using cin function...

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

Upload: hollie-cunningham

Post on 29-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

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

Operating System• Using setw and setprecision functions

• Using setiosflags function• Using cin function

Programming 1DCT 1033

1

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

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

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

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

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

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

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

Input (Read) Statement (continued)

Must be included to use cin

So we can refer to cin without using std::

55

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

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 statementcin >> feet;• More versatile

66

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

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

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

88

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

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

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

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

1010

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

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;

1111

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

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

1212

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

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

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

Input Failure (continued)

1414

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

Input Failure (continued)

• Sample run 1Line 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 2Line 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

1515

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

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

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

17

setprecision Manipulator

• Syntax is:

• Outputs decimal numbers with up to n decimal places

• Must include the header file iomanip:#include <iomanip>

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

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

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

19

showpoint Manipulator

• showpoint forces output to show the decimal point and trailing zeros

• Examplecout << fixed << showpoint;

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

2020

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

showpoint Manipulator (continued)

• Sample runLine 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

2121

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

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

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

2323

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

setw (continued)

• Sample run12345678901234567890

19

345 Hi 19

345 76.38 19

19 345 76.38

34519

345 19

2424

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

25

left and right Manipulators

• left: left-justifies the output

• right: right-justifies the output

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

left and right Manipulators (continued)

2626

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

left and right Manipulators (continued)

• Sample run12345678901234567890

15 7634 Warm

15 7634 Warm

2727

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

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

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

29

Summary (continued)

• setprecision, fixed, showpoint, setw, left, and right can be used to format output

• Include iomanip to use setprecision and setw