how to implement a user-defined exception class for this program #include <iostr
ID: 3819591 • Letter: H
Question
how to implement a user-defined exception class for this program
#include <iostream>
#include <string>
#include <exception>
using namespace std;
const double Inches_in_centimeter = 2.54;
const int feet_in_inch = 12;
int main()
{
int feet;
int inches;
int totalinches; //totalinches = feet_in_inch * feet + inches//
double centimeter;
bool done = false;
string negativeInput ="Invalid input: Do not enter negative number or non-digit number ";
do
{
try
{
cout << "Enter the length in feet: ";
cin >> feet;
if (feet <=0)
throw negativeInput;
cout << "Enter length in inches: ";
cin >> inches;
if (inches <=0)
throw negativeInput;
else if (isalpha(feet)!= 0 ||
isalpha(inches)!= 0)
throw negativeInput;
done = true;
totalinches = feet_in_inch * feet + inches;
centimeter = Inches_in_centimeter * totalinches;
cout << "The length in centimeter is: "<<centimeter << endl;
}
catch (string inputError)
{
cout << inputError << endl;
cin.clear();
}
} while(!done);
return 0;
}
Explanation / Answer
User Defind function can be written by inheriting exception class and the implemented example given below
#include<iostream>
#include<exception>
using namespace std;
class NegativeInput: public exception //User defind exception NegativeInput inherits from exception
{
public:
const char * checnegetivevalue() const throw()
{
return "Invalid input: Do not enter negative number or non-digit number "; //Exception string user defind
}
};
int main()
{
int feet;
int inches;
int totalinches; //totalinches = feet_in_inch * feet + inches//
int Inches_in_centimeter;
double centimeter;
int feet_in_inch;
bool done = false;
do
{
try
{
NegativeInput ninput; //Defined ninput as user defind exception object
cout << "Enter the length in feet: ";
cin >> feet;
if (feet <=0)
{
throw ninput; //Throwing USer defind Exception
}
cout << "Enter length in inches: ";
cin >> inches;
if (inches <=0)
throw ninput;
else if (isalpha(feet)!= 0 ||
isalpha(inches)!= 0)
throw ninput;
done = true;
//Added code for input feet_in_inch and Inches_in_centimeter
cout << "Enter length in feet_in_inch: ";
cin >> feet_in_inch;
if (feet_in_inch<=0)
{
throw ninput; //Throwing User Defind exception
}
cout << "Enter length in Inches_in_centimeter: ";
cin >> Inches_in_centimeter;
if (Inches_in_centimeter<=0)
{
throw ninput;
}
totalinches = feet_in_inch * feet + inches;
centimeter = Inches_in_centimeter * totalinches;
cout << "The length in centimeter is: "<<centimeter << endl;
}
catch (NegativeInput e) //Handeling User Defind Exception
{
cout << "inputError"<<e.checnegetivevalue()<< endl;
cin.clear();
}
} while(!done);
return 0;
}
Executed O/P
$ ./NegativeInput
Enter the length in feet: 0
inputErrorInvalid input: Do not enter negative number or non-digit number
Enter the length in feet: 0
inputErrorInvalid input: Do not enter negative number or non-digit number
Enter the length in feet: 9
Enter length in inches: 8
Enter length in feet_in_inch: 0
inputErrorInvalid input: Do not enter negative number or non-digit number
admin@admin-PC ~/cplusplus
$ vi NegativeInput.cpp
admin@admin-PC ~/cplusplus
$ ./NegativeInput
Enter the length in feet: 7
Enter length in inches: 7
Enter length in feet_in_inch: 7
Enter length in Inches_in_centimeter: 8
The length in centimeter is: 448
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.