-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp.cpp
More file actions
37 lines (34 loc) · 723 Bytes
/
Copy pathexp.cpp
File metadata and controls
37 lines (34 loc) · 723 Bytes
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
#include <iostream>
double calculate_mpg(int miles,int gallon){
if (gallon==0)
{
throw 0;
}
if (miles<0||gallon<0)
{
throw std::string("negative number");
}
return static_cast<double>(miles)/gallon;
}
int main(){
int miles;
int gallon;
double miles_per_gallon;
std::cout<<" enter miles ";
std::cin>>miles;
std::cout<<" enter gallon ";
std::cin>>gallon;
try{
miles_per_gallon = calculate_mpg(miles,gallon);
std::cout <<" Result: "<<miles_per_gallon<<std::endl;
}
catch(int ex){
std::cout<<ex;
std::cerr<<" tried to divide by zero"<<std::endl;
}
catch(std::string ex){
std::cerr<<ex<<std::endl;
}
std::cout<<"Bye"<<std::endl;
return 0;
}