-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 14.5
56 lines (41 loc) · 3.05 KB
/
Exercise 14.5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
1. (Practice) Write a C++ program that prompts the user to enter an integer. Have your program
use cin to accept the number as an integer and use cout to display the value your program
actually accepted from the data entered. Run your program four times. The first time you run
the program, enter a valid integer number; the second time, enter a double-precision number;
the third time, enter a character; and the fourth time, enter the value 12e34.
2. (Modify) Modify the program you wrote for Exercise 1 to prompt for and accept a doubleprecision
number. Run the program four times: First, enter an integer; second, enter a decimal
number; third, enter a decimal number with an “f” as the last character entered; and fourth,
enter a character. Using the output display, keep track of the number your program actually
accepted from the data you entered. What happened, if anything, and why?
3. (For thought) a. Why do you think successful application programs contain extensive data
input validity checks? (Hint: Review Exercises 1 and 2.)
b. What do you think the difference is between a data-type check and a data-reasonableness
check?
c. A program requests that the user enter a month, day, and year. What are some reasonable
checks that could be made on the data entered?
4. (Practice) a. Enter and run Program 14.18.
b. Run Program 14.18 four times, using the data listed in Exercise 1 for each run.
5. (Modify) Modify Program 14.18 to display any invalid characters that were entered.
6. (Modify) Modify Program 14.18 to request an integer continuously until a valid number is
entered.
7. (Modify) Modify Program 14.18 to remove all leading and trailing spaces from the entered
string before it’s checked for validity.
8. (Useful utility) Write a function that checks each digit as it’s entered, instead of checking the
completed string, as in Program 14.18.
9. (Practice) Enter and run Program 14.19.
10. (For thought) Discuss whether the isvalidInt() function used in Program 14.19 should be
modified to accept a string that ends in a decimal point. For example, should the input 12. be
accepted and converted to the integer number 12 or simply be rejected as noninteger input?
11. (Useful utility) a. Write a C++ function named isvalidReal() that checks for a valid
double-precision number. This kind of number can have an optional + or - sign, at most one
decimal point (which can be the first character), and at least one digit between 0 and 9. The
function should return the Boolean value true if the entered number is a real number; otherwise,
it should return the Boolean value false.
b. Modify the isvalidReal() function written for Exercise 11a to remove all leading and
trailing blank spaces from its string argument before determining whether the string corresponds
to a valid real number.
12. (Useful utility) Write and test a C++ function named getaReal() that uses exception handling
to accept an input string continuously until a string that can be converted to a real number
is entered. This function should return a double value corresponding to the string value
the user enters.