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

Download Report

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

Programming 1
DCT 1033
Operating System
• Using setw and setprecision functions
• Using setiosflags function
• Using cin function
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
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
3
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
4
Input (Read) Statement (continued)
Must be included to use cin
So we can refer to cin without using std::
5
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
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;
}
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
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
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;
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
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
13
Input Failure (continued)
14
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
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
16
setprecision Manipulator
• Syntax is:
• Outputs decimal numbers with up to n decimal
places
• Must include the header file iomanip:
#include <iomanip>
17
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
18
showpoint Manipulator
• showpoint forces output to show the decimal
point and trailing zeros
• Example
cout << fixed << showpoint;
19
20
showpoint Manipulator (continued)
• Sample run
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
setprecision(2)
x = 15.67
y = 235.73
z = 9525.99
setprecision(3)
x = 15.674
y = 235.730
z = 9525.986
setprecision(4)
x = 15.6740
y = 235.7300
z = 9525.9864
21
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
22
23
setw (continued)
• Sample run
12345678901234567890
19
345
Hi
19
345 76.38
19
19
345 76.38
34519
345 19
24
left and right Manipulators
• left: left-justifies the output
• right: right-justifies the output
25
left and right Manipulators
(continued)
26
left and right Manipulators
(continued)
• Sample run
12345678901234567890
15
7634
Warm
15
7634
Warm
27
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
28
Summary (continued)
• setprecision, fixed, showpoint, setw,
left, and right can be used to format output
• Include iomanip to use setprecision and setw
29