Title: InputOutput Control
1Input/Output Control
2User Input Error Handling
- void main(void)
-
- int a
- cout ltlt "Enter a number "
- cin gtgt a
- cout ltlt "The number is " ltlt a ltlt endl
-
Enter a number 197 The number is 197
When an incorrect input is entered the program
continues to process the input.
Enter a number dog The number is 0
3User Input Error Handling
- To detect invalid user entries we can use the
function - bool cin.fail(void)
- This function returns a value of type bool and
requires no input argument. The return value is
true when the user input is invalid.
4User Input Error Handling
- void main(void)
-
- int a
- cout ltlt "Enter a number "
- cin gtgt a
- if (cin.fail())
- cout ltlt "Invalid entry ltlt endl
- else
- cout ltlt "The number is " ltlt a ltlt endl
-
-
Enter a number dog Invalid entry
5User Input Error Handling
- If a user enters something invalid, we typically
would like to prompt the user again to re-enter
their data. - Note that keystrokes are buffered, and when an
error occurs, the offending keystrokes (in this
example dog) remain in this buffer causing
subsequent calls to cin gtgt a to also fail.
6User Input Error Handling
- void main(void)
-
- int a
- while (1)
- cout ltlt "Enter a number "
- cin gtgt a
- cout ltlt "The number is " ltlt a ltlt endl
- if (!cin.fail())
- break
-
-
-
Enter a number dog The number is 0 Enter a
number The number is 0 Enter a number The
number is 0 Enter a number The number is 0 Enter
a number The number is 0
An infinite loop has been created. The computer
continuously displays this line and the user is
not provided the opportunity to re-enter the data
correctly
7User Input Error Handling
- To properly implement a re-prompting, it is
necessary to clear the input buffer prior to the
next call to cin gtgt a. This is accomplished
using - cin.ignore(1000,'\n')
- This call discards at most 1000 keystrokes in the
input buffer, up to the next carriage return. - In addition to clearing the input buffer, it is
also necessary to clear the error flag in cin
using - cin.clear()
8User Input Error Handling
- void main(void)
-
- int a
- while (1)
- cout ltlt "Enter a number "
- cin gtgt a
- if (!cin.fail())
- break
-
- cout ltlt "Invalid entry. Try again. ltlt endl
- cin.clear()
- cin.ignore(1000,\n)
-
- cout ltlt "The number is " ltlt a ltlt endl
-
Enter a number dog Invalid entry. Try
again. Enter a number 127 The number is 127
9Formatting Program Output
- It is often desirable to format the output of
your program to improve the readability of the
output.
Poor
Good
Item Price Banana 0.9 Cheese 4.39 Carrot 1.0
Item Price Banana 0.90 Cheese 4.39 Carrot
1.00
In the example shown here, the left box shows
items and prices with the columns that do not
line up. The listed prices are shown poorly as
well because some of them only have a single
digit after the decimal point, where we would
expect two. The right box is a major
improvement. For output formatting you must have
the directive include ltiomanip.hgt which provides
many of the formatting functionality.
10Formatting Field Width
- Everything you display using cout is put in
memory block as a field. By default, the size of
the field is equal to the minimum size needed to
display the field contents.
int a 51, b 127 cout ltlt a " ltlt a ltlt ""
ltlt endl cout ltlt b " ltlt b ltlt "" ltlt endl
a 51 b 127
If we precede displaying the field using the
function setw(int) we can change the minimum
field width of the next field.
int a 51, b 127 cout ltlt a " ltlt setw(4) ltlt
a ltlt "" ltlt endl cout ltlt b " ltlt setw(4) ltlt b
ltlt "" ltlt endl
a 51 b 127
In the example shown above, the variable a is
padded with two spaces and the variable b is
padded with a single space. In both cases, this
results in a field width of 4. Note we need to
include ltiomanip.hgt to enable the setw
functionality.
11Formatting Field Width
- If the field width is larger than the specified
minimum field size, it will still be properly
displayed.
int a 51, b 12726 cout ltlt a " ltlt setw(4)
ltlt a ltlt "" ltlt endl cout ltlt b " ltlt setw(4) ltlt
b ltlt "" ltlt endl
a 51 b 12726
By default character padding is accomplished by
using spaces to the left of the field. This can
be changed using the cout.fill(char) function.
int a 51, b 127 cout.fill(x) cout ltlt a
" ltlt setw(4) ltlt a ltlt "" ltlt endl cout.fill(0)
cout ltlt b " ltlt setw(4) ltlt b ltlt "" ltlt endl
a xx51 b 0127
12Formatting Field Width
- By default the fields are displayed with
right-justification (i.e. fields are padded by
adding characters on the left).
Left-justification (i.e. fields padded by adding
characters on the right) can be accomplished
using the function cout.setf(iosleft)
int a 51 cout.fill(x) cout ltlt a " ltlt
setw(4) ltlt a ltlt "" ltlt endl cout.setf(iosleft)
cout ltlt a " ltlt setw(4) ltlt a ltlt "" ltlt endl
a xx51 a 51xx
This justification remains in effect until a call
to the function cout.unsetf(iosleft) is made
13Formatting Real Numbers
- When displaying floating-point numbers, cout does
not display redundant zeros after the decimal
point.
double a 27.500, b 30.0000 cout ltlt a ltlt endl
ltlt b ltlt endl
27.5 30
Using the function cout.setf(iosfixed) you can
force cout to display a fixed number of digits
after the decimal point. This remains in effect
until a call to cout.unsetf(iosfixed) is made.
double a 27.500, b 30.0000 cout.setf(iosfix
ed) cout ltlt a ltlt endl ltlt b ltlt endl
27.500000 30.000000
14Formatting Real Numbers
- The number of fixed digits after the decimal
point to display can be changd by calling
cout.precision(int).
double a 27.500, b 30.0000 cout.setf(iosfix
ed) cout.precision(2) cout ltlt ltlt a ltlt endl
ltlt ltlt b ltlt endl
27.50 30.00
If the number to be displayed has more digits
than the specified number of digits after the
decimal point to display, the number is rounded.
double a 27.503, b 30.0070 cout.setf(iosfix
ed) cout.precision(2) cout ltlt ltlt a ltlt endl
ltlt ltlt b ltlt endl
27.50 30.01
The function setw(int) can be used to specify the
minimum field width for floating-point numbers,
as it can with any field.
15Formatting Real Numbers
- Numbers that are very large or very small are
best displayed in scientific notation. By default
cout does this automatically for very large and
very small numbers.
double c 300000000, d 0.000000000001244 cout
ltlt c ltlt endl ltlt d ltlt endl
3e08 1.244e-12
Scientific notation can be used in your code as
well.
double c 3e8, d 1.244e-12 cout ltlt c ltlt endl
ltlt d ltlt endl
3e08 1.244e-12
Scientific notation can be used to display all
variables by using the function
cout.setf(iosscientific).
cout.setf(iosscientific) double c -27.234, d
0.0351 cout ltlt c ltlt endl ltlt d ltlt endl
-2.734e01 3.51e-02
16Formatting Real Numbers
- You can set the number of digits displayed after
the decimal using cout.precision(int)
double c 300000000, d 0.000000000001244 cout.
precision(2) cout ltlt c ltlt endl ltlt d ltlt endl
3.00e08 1.24e-12
You can use setw(int) to set the minimum field
width.
double c 300000000, d 0.000000000001244 cout.
precision(2) cout.fill(x) cout ltlt c ltlt endl
ltlt setw(10) ltlt d ltlt endl
3.00e08 xx1.24e-12
17Manipulators
- The cout functions described in previous pages
can also be implemented using manipulators, which
are shorthand methods. The following two blocks
of code are equivalent.
double a 5.7 cout.setf(iosfixed) cout.precis
ion(2) cout.fill(0) cout ltlt setw(6) ltlt a ltlt
endl
005.70
double a 5.7 cout ltlt setiosflags(iosfixed)
ltlt setprecision(2) ltlt setfill(0)
ltlt setw(6) ltlt a ltlt endl
005.70
18Problem 1 Product
Write a program that takes a user input (type
double) and determines its product with the
numbers 1 to 20. Format the results to so that
numbers are shown in scientific notation, with 2
decimal places. Note each column is 16 characters
wide.
- Enter a number 123.456
- Number Multiplier Product
- ------ ---------- -------
- 1.23e002 1.00e000 1.23e002
- 1.23e002 2.00e000 2.47e002
- 1.23e002 3.00e000 3.70e002
- 1.23e002 4.00e000 4.94e002
- 1.23e002 5.00e000 6.17e002
- 1.23e002 6.00e000 7.41e002
- 1.23e002 7.00e000 8.64e002
- 1.23e002 8.00e000 9.88e002
- 1.23e002 9.00e000 1.11e003
- 1.23e002 1.00e001 1.23e003
- 1.23e002 1.10e001 1.36e003
- 1.23e002 1.20e001 1.48e003
- 1.23e002 1.30e001 1.60e003
- 1.23e002 1.40e001 1.73e003
- 1.23e002 1.50e001 1.85e003
- 1.23e002 1.60e001 1.98e003
19Problem 2 Cash register
Suppose there are twelve numbers of items to
purchase (use a for loop and an array). Write a
program that accepts each item price and the
number of each item, and then displays that
information, the total price for that item and
the total cost for all the items. Format the
output as shown below. Continued on next page
- Enter the cost for item 1 0.90
- How many of item 1 is being purchased 10
- Enter the cost for item 2 12.21
- How many of item 2 is being purchased 3
-
- Enter the cost for item 12 0.86
- How many of item 12 is being purchased 5
- Item Price per Number purchased Total
- ---- --------- ---------------- -----
- 1 0.90 10 9.00
- 2 12.21 3 36.63
-
- 12 0.86 5 4.30
- ------
- Total 129.45
20Problem 2 Cash register
- You should have a function that obtains and
validates the data needed for item i. The
function should check that the itemPrice and
itemQuantity were numbers greater than or equal
to zero. If the value enter is not a number of is
not greater than or equal to zero the user should
be re-prompted to re-enter the data. - void getItemData(int itemNumber, float
itemPrice, int itemQuantity) - You should have a function that computes the
total price for item i - double computeTotalPrice(double itemPrice, int
itemQuantity) - You should have a function that displays the
table information all of the items. Include the
table heading and grand total. - void displayItemData(const int NUM_ITEMS, double
itemPrice, int itemQuantity, double
totalPrice)