-
Notifications
You must be signed in to change notification settings - Fork 0
/
error5.cpp
90 lines (85 loc) · 1.69 KB
/
error5.cpp
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
// error5.cpp -- unwinding the stack
#include <iostream>
#include <cmath>
#include <string>
#include "exe_mean.h"
class demo{
private:
std::string word;
public:
demo(const std::string & str){
word=str;
std::cout<<"demo "<<word<<" created\n";
}
~demo(){
std::cout<<"demo "<<word<<" destroyed\n";
}
void show(){
std::cout<<"demo "<<word<<" lives!\n";
}
};
// function prototypes
double hmean(double a, double b);
double gmean(double a, double b);
double means(double a, double b);
int main(){
using std::cout;
using std::cin;
using std::endl;
double x, y, z;
{
demo d1("found in block in main()");
cout<<"Enter two numbers: ";
while(cin>>x>>y){
try{
z=means(x, y);
cout<<"The mean of "<<x<<" and "<<y
<<" is "<<z<<endl;
cout<<"Enter next pair: ";
}
catch(bad_hmean & bg){
bg.mesg();
cout<<"Try again.\n";
continue;
}
catch(bad_gmean & hg){
cout<<hg.mesg();
cout<<"Values used: "<<hg.v1<<", "
<<hg.v2<<endl;
cout<<"Sorry, you don't get to play any more.\n";
break;
}
}
d1.show();
}
cout<<"Bye!\n";
cin.get();
cin.get();
return 0;
}
double hmean(double a, double b){
if(a == -b)
throw bad_hmean(a, b);
return 2.0*a*b/(a+b);
}
double gmean(double a, double b){
if(a<0 || b<0)
throw bad_gmean(a, b);
return std::sqrt(a*b);
}
double means(double a, double b){
double am,hm,gm;
demo d2("found in means()");
am=(a+b)/2.0;
try{
hm=hmean(a, b);
gm=gmean(a, b);
}
catch(bad_hmean & bg){
bg.mesg();
std::cout<<"Cauth in means()\n";
throw;
}
d2.show();
return (am+hm+gm)/3.0;
}