input and output using setw and setprecision functions using setiosflags function
DESCRIPTION
PROGRAMMING FUNDAMENTALS. INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function Using cin function. Objectives. The topic will be cover: Assignment Operations Formatting Numbers for Program Output Using Mathematical Library Functions - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/1.jpg)
INPUT AND OUTPUTUsing setw and setprecision functions
Using setiosflags function
Using cin function
PROGRAMMING PROGRAMMING FUNDAMENTALSFUNDAMENTALS
![Page 2: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/2.jpg)
2
ObjectivesObjectives
The topic will be cover:• Assignment Operations • Formatting Numbers for Program Output• Using Mathematical Library Functions• Program Input Using the cin Object• Symbolic Constants
![Page 3: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/3.jpg)
3
Objectives (continued)Objectives (continued)
• Applications• Common Programming Errors• A Closer Look: Programming Errors
![Page 4: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/4.jpg)
4
Assignment OperationsAssignment Operations
• Assignment Statement: assigns the value of the expression on the right side of the = to the variable on the left side of the =
• Another assignment statement using the same variable will overwrite the previous value with the new valueExamples:
slope = 3.7;
slope = 6.28;
![Page 5: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/5.jpg)
5
Assignment Operations (continued)Assignment Operations (continued)
• Right side of an assignment statement may contain any expression that can be evaluated to a valueExamples:
newtotal = 18.3 + total;taxes = .06*amount;average = sum / items;
• Only one variable can be on the left side of an assignment statement
![Page 6: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/6.jpg)
6
Assignment Operations (continued)Assignment Operations (continued)
![Page 7: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/7.jpg)
7
Assignment Operations (continued)Assignment Operations (continued)
• Assignment operator: the = sign• C++ statement: any expression terminated by
a semicolon• Multiple assignments in the same expression
are possibleExample:
a = b = c = 25;
![Page 8: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/8.jpg)
8
Assignment Operations (continued)Assignment Operations (continued)
• Coercion: forcing a data value to another data type
• Value of the expression on the right side of an assignment statement will be coerced (converted) to the data type of the variable on the left side during evaluation
• Variable on the left side may also be used on the right side of an assignment statement
![Page 9: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/9.jpg)
9
Assignment Operations (continued)Assignment Operations (continued)
![Page 10: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/10.jpg)
10
Assignment Operations (continued)Assignment Operations (continued)
• Additional assignment operators provide short cuts: +=, -=, *=, /=, %=Example:
sum = sum + 10;
is equivalent to: sum += 10;
price *= rate +1;
is equivalent to:
price = price * (rate + 1);
![Page 11: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/11.jpg)
11
Assignment Operations (continued)Assignment Operations (continued)
• Accumulation statement: has the effect of accumulating, or totalingSyntax:
variable = variable + newValue;
![Page 12: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/12.jpg)
12
Assignment Operations (continued)Assignment Operations (continued)
![Page 13: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/13.jpg)
13
Assignment Operations (continued)Assignment Operations (continued)
• Counting statement: adds a fixed value to the variable’s current valueSyntax:
variable = variable + fixedNumber;Example:
i = i + 1;count = count + 1;
![Page 14: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/14.jpg)
14
Assignment Operations (continued)Assignment Operations (continued)
• Increment operator ++: unary operator for the special case when a variable is increased by 1
• Prefix increment operator appears before the variableExample: ++i
• Postfix increment operator appears after the variableExample: i++
![Page 15: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/15.jpg)
15
Assignment Operations (continued)Assignment Operations (continued)
Example: k = ++n; //prefix increment
is equivalent to
n = n + 1; //increment n first
k = n; //assign n’s value to k
Example: k = n++; //postfix increment
is equivalent to
k = n; //assign n’s value to k
n = n + 1; //and then increment n
![Page 16: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/16.jpg)
16
Assignment Operations (continued)Assignment Operations (continued)
• Decrement operator --: unary operator for the special case when a variable is decreased by 1
• Prefix decrement operator appears before the variableExample: --i;
• Postfix decrement operator appears after the variableExample: i--;
![Page 17: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/17.jpg)
17
Formatting Numbers Formatting Numbers for Program Outputfor Program Output
• Proper output formatting contributes to ease of use and user satisfaction
• cout with stream manipulators can control output formatting
![Page 18: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/18.jpg)
18
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
Commonly Used Stream Manipulators
![Page 19: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/19.jpg)
19
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
![Page 20: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/20.jpg)
20
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
• The field width manipulator must be included for each value in the data stream sent to cout
• Other manipulators remain in effect until they are changed
• iomanip header file must be included to use manipulators requiring arguments
![Page 21: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/21.jpg)
21
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
• Formatting floating-point numbers requires three field-width manipulators to– Set the total width of the display
– Force a decimal place
– Set the number of significant digits after the decimal point
Example:
produces this output:
![Page 22: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/22.jpg)
22
![Page 23: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/23.jpg)
23
• Sample run
12345678901234567890
15 7634 Warm
15 7634 Warm
![Page 24: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/24.jpg)
24
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
• setprecision: sets the number of digits after the decimal point if a decimal point has been explicitly forced; otherwise, it sets the total number of displayed digits
• If the field width is too small, cout ignores the setw manipulator setting and allocates enough space for printing
• If setprecision setting is too small, the fractional part of the value is rounded to the specified number of decimal places
![Page 25: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/25.jpg)
25
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
• If setprecision value is too large, the fractional value is displayed with its current size
![Page 26: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/26.jpg)
26
![Page 27: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/27.jpg)
27
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
Effect of Format Manipulators
![Page 28: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/28.jpg)
28
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
Effect of Format Manipulators (continued)
![Page 29: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/29.jpg)
29
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
• setiosflags manipulator: allows additional formatting:– Right or left justification – Fixed display with 6 decimal places – Scientific notation with exponential display – Display of a leading + sign
• Parameterized manipulator: one which requires arguments, or parameters
![Page 30: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/30.jpg)
30
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
Format Flags for Use with setiosflags()
![Page 31: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/31.jpg)
31
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
![Page 32: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/32.jpg)
32
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
• To designate an octal integer constant, use a leading zero
• To designate a hexadecimal integer constant, use a leading 0x
• Manipulators affect only output; the value stored internally does not change
![Page 33: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/33.jpg)
33
Formatting Numbers for Program Formatting Numbers for Program Output (continued)Output (continued)
![Page 34: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/34.jpg)
34
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
• Manipulators can also be set using the ostream class methods
• Separate the cout object name from the method name with a periodExample:
cout.precision(2)
![Page 35: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/35.jpg)
35
Formatting Numbers for Formatting Numbers for Program Output (continued)Program Output (continued)
Table 3.4 ostream Class Methods
![Page 36: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/36.jpg)
36
Using Mathematical Library FunctionsUsing Mathematical Library Functions
• C++ has preprogrammed mathematical functions that can be included in a program
• You must include the cmath header file:
#include <cmath>
• Math functions require one or more arguments as input, but will return only one value
• All functions are overloaded, and can be used with integer and real arguments
![Page 37: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/37.jpg)
37
Using Mathematical Library Functions Using Mathematical Library Functions (continued)(continued)
Common C++ Functions
![Page 38: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/38.jpg)
38
Using Mathematical Library Functions Using Mathematical Library Functions (continued)(continued)
• To use a math function, give its name and pass the input arguments within parentheses
• Expressions that can be evaluated to a value can be passed as arguments
![Page 39: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/39.jpg)
39
Using Mathematical Library Functions Using Mathematical Library Functions (continued)(continued)
![Page 40: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/40.jpg)
40
Using Mathematical Library Functions Using Mathematical Library Functions (continued)(continued)
• Function calls can be nestedExample: sqrt(sin(abs(theta)))
• Cast operator: a unary operator that forces the data to the desired data type
• Compile-time castSyntax: dataType (expression)
Example: int(a+b)
![Page 41: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/41.jpg)
41
Using Mathematical Library Functions Using Mathematical Library Functions (continued)(continued)
• Run-time cast: the requested conversion is checked at run time and applied if validSyntax:
staticCast<data-type> (expression)
Example:
staticCast<int>(a*b)
![Page 42: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/42.jpg)
42
Program Input Using the Program Input Using the cincin Object Object
• cin Object: allows data entry to a running program
• Use of the cin object causes the program to wait for input from the keyboard
• When keyboard entry is complete, the program resumes execution, using the entered data
• An output statement preceding the cin object statement provides a prompt to the user
![Page 43: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/43.jpg)
43
Program Input Using the Program Input Using the cincin Object Object (continued)(continued)
![Page 44: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/44.jpg)
44
Program Input Using the Program Input Using the cincin Object Object (continued)(continued)
• cin can accept multiple input values to be stored in different variables
• Multiple numeric input values must be separated by spacesExample:
cin >> num1 >> num2
with keyboard entry: 0.052 245.79
![Page 45: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/45.jpg)
45
Program Input Using the Program Input Using the cincin Object Object (continued)(continued)
![Page 46: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/46.jpg)
46
Program Input Using the Program Input Using the cincin Object Object (continued)(continued)
• User-input validation: the process of ensuring that data entered by the user matches the expected data type
• Robust program: one that detects and handles incorrect user entry
![Page 47: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/47.jpg)
47
Symbolic ConstantsSymbolic Constants
• Symbolic constant: a constant value that is declared with an identifier using the const keyword
• A constant’s value may not be changedExample:
const int MAXNUM = 100;
• Good programming places statements in appropriate order
![Page 48: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/48.jpg)
48
Symbolic Constants (continued)Symbolic Constants (continued)
• Proper placement of statements:preprocessor directives
int main()
{
symbolic constants
main function declarations
other executable statements
return value
}
![Page 49: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/49.jpg)
49
#include<iostream.h>int main(){const int MAXNUMS = 4;int count;float num, total, average;cout<< “ \nThis Program will ask you to enter”<< MAXNUMS<<”numbers.\n”;count = 1;total= 0;while (count <= MAXNUMS){cout << “Enter a number:”;cin>> num;total= total + num;average = total / count;count++;}
![Page 50: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/50.jpg)
50
Common Programming ErrorsCommon Programming Errors
• Failure to declare or initialize variables before use
• Failure to include the preprocessor statement when using a C++ preprogrammed library
• Passing the incorrect number or type of arguments to a function
• Applying increment or decrement operator to an expression instead of an individual variable
![Page 51: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/51.jpg)
51
Common Programming Errors Common Programming Errors (continued)(continued)
• Failure to separate all variables passed to cin with the extraction symbol >>
• Failure to test thoroughly• Compiler-dependent evaluation when
increment or decrement operators are used with variables that appear more than once in the same expression
![Page 52: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/52.jpg)
52
SummarySummary
• Expression: a sequence of one or more operands separated by operators
• Expressions are evaluated based on precedence and associativity
• Assignment operator: =• Increment operator: ++• Decrement operator: --
![Page 53: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/53.jpg)
53
Summary (continued)Summary (continued)
• Use #include <cmath> for math functions• Arguments to a function must be passed in the
proper number, type, and order• Functions may be included within larger
expressions• cin object provides data input from a
keyboard; program is suspended until the input arrives
![Page 54: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/54.jpg)
54
Summary (continued)Summary (continued)
• Use a prompt to alert the user to provide input• Constants are named values that do not
change
![Page 55: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/55.jpg)
55
A Closer Look: Programming ErrorsA Closer Look: Programming Errors
• Program errors may be detected in four ways:– Before a program is compiled (desk checking)– While it is being compiled (compile-time
errors)– While it is being run (run-time errors)– While examining the output after completion
• Errors may be– Typos in the source code– Logic errors
![Page 56: INPUT AND OUTPUT Using setw and setprecision functions Using setiosflags function](https://reader036.vdocuments.site/reader036/viewer/2022062422/5681373f550346895d9ed329/html5/thumbnails/56.jpg)
56
A Closer Look: Programming ErrorsA Closer Look: Programming Errors
• Logic errors: often difficult to detect, and difficult to find the source
• Program tracing: stepping through the program by hand or with a trace tool
• Debugger: program that allows the interruption of a running program to determine values of its variables at any point