-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 14.3 (Completed)
172 lines (155 loc) · 5.23 KB
/
Exercise 14.3 (Completed)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
1. (Practice) Define the following terms:
a. Exception - is a value, a variable, or an object containing information about the error at the point the error occurs
b. try block - identifies the start of an exception-handling block of code.
c. catch block - receive a thrown exception and process it
d. Exception handler - when an error occurs while a function is executing, the function creates an exception.
e. Throw an exception - the process of generating and passing an exception
f. Catch an exception - receive a thrown exception and process it
2. (Practice) Enter and run Program 14.14.
//Done!!
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int numerator, denominator;
bool needDenominator = true;
cout << "Enter a numerator(whole number only) : ";
cin >> numerator;
cout << "Enter a denominator(whole number only) : ";
while (needDenominator)
{
cin >> denominator;
try
{
if (denominator == 0)
throw denominator; // an integer value is thrown
}
catch (int e)
{
cout << "A denominator value of " << e << " is invalid." << endl;
cout << "Please reenter the denominator(whole number only) : ";
continue; // send control back to the while statement
}
cout << numerator << '/' << denominator
<< " = " << double(numerator) / double(denominator) << endl;
needDenominator = false;
}
system("PAUSE");
return 0;
}
3. (Modify) Replace the following statement in Program 14.14
cout << numerator <<'/' << denominator
<< “ = “ << double (numerator)/ double (denominator) << endl;
with the statement
cout << numerator <<'/' << denominator
<< “ = “ << numerator/denominator << endl;
and run the modified program. Enter the values 12 and 5, and explain why the result is incorrect
from the user’s viewpoint.
//Done!!
The results are incorrect because they do not contain the decimal point information required for accurate calcuations.
4. (Modify) Modify Program 14.14 so that it throws and catches the message ***Invalid input
-A denominator value of zero is not permitted***. (Hint: Review the caution at
the end of this section.)
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int numerator, denominator;
bool needDenominator = true;
cout << "Enter a numerator(whole number only) : ";
cin >> numerator;
cout << "Enter a denominator(whole number only) : ";
while (needDenominator)
{
cin >> denominator;
try
{
if (denominator == 0)
throw "***Invalid input - A zero denominator value is not permitted***"; // an integer value is thrown
}
catch (char e[])
{
cout << "A denominator value of " << e << " is invalid." << endl;
cout << "Please reenter the denominator(whole number only) : ";
continue; // send control back to the while statement
}
cout << numerator << '/' << denominator
<< " = " << double(numerator) / double(denominator) << endl;
needDenominator = false;
}
system("PAUSE");
return 0;
}
5. (Modify) Modify Program 14.14 so that the try and catch blocks are included in a while
statement. The while statement should provide code that continuously requests the user to
enter a denominator until a non-zero number is entered. (Hint: The prompt to enter a new
denominator should be made in the catch block immediately after the message informing the
user an invalid denominator has been entered.)
//Done Error program 14.14 try-catch method is already in a while loop that performs the required actions.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int numerator, denominator;
bool needDenominator = true;
cout << "Enter a numerator(whole number only) : ";
cin >> numerator;
cout << "Enter a denominator(whole number only) : ";
while (needDenominator)
{
cin >> denominator;
try
{
if (denominator == 0)
throw "***Invalid input - A zero denominator value is not permitted***"; // an integer value is thrown
}
catch (char e[])
{
cout << "A denominator value of " << e << " is invalid." << endl;
cout << "Please reenter the denominator(whole number only) : ";
continue; // send control back to the while statement
}
cout << numerator << '/' << denominator
<< " = " << double(numerator) / double(denominator) << endl;
needDenominator = false;
}
system("PAUSE");
return 0;
}
6. (Modify) Modify Program 14.14 so that it continues to divide two numbers until the user
enters the number 999 (as a numerator or denominator) to terminate program execution.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int numerator, denominator;
bool needDenominator = true;
cout << "Enter a numerator(whole number only) : ";
cin >> numerator;
cout << "Enter a denominator(whole number only) : ";
while (needDenominator)
{
cin >> denominator;
try
{
if (denominator == 0)
throw denominator; // an integer value is thrown
}
catch (int e)
{
cout << "A denominator value of " << e << " is invalid." << endl;
cout << "Please reenter the denominator(whole number only) : ";
continue; // send control back to the while statement
}
cout << numerator << '/' << denominator
<< " = " << double(numerator) / double(denominator) << endl;
if (numerator == 999 || denominator == 999){
needDenominator = false; }
}
system("PAUSE");
return 0;
}