Redo Programming Exercise 7 of Chapter 8 so that your progress random exception
ID: 3627174 • Letter: R
Question
Redo Programming Exercise 7 of Chapter 8 so that your progress random exception such as division by zero and invalid input Write a program that prompts the user to enter time in 12-bour solution The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalider, invalidation, and invalisec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Similar conventions for the invalid values of minutes and seconds. Write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception calsses: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
char menu();
void addFractions( int a1, int b1, int a2, int b2,int& num, int& den );
void subtractFractions( int a1, int b1, int a2, int b2,int& num, int& den );
void multiplyFractions( int a1, int b1, int a2, int b2,int& num, int& den );
void divideFractions( int a1, int b1, int a2, int b2,int& num, int& den );
int main()
{
int a1, b1, a2, b2, num, den;
char ch = menu();
try
{
cout << " Enter the numerator of fraction1: ";
cin >> a1;
cout << " Enter the denominator of fraction1: ";
cin >> b1;
if ( b1 == 0 )
throw b1;
else if(b1<0)
{
string s="Negative denominator Exception";
throw s;
}
cout << " Enter the numerator of fraction2: ";
cin >> a2;
cout << " Enter the denominator of fraction2: ";
cin >> b2;
if ( b2 == 0 )
throw b2;
else if(b2<0)
{
string s="Negative denominator Exception";
throw s;
}
switch ( ch )
{
case '+':
addFractions(a1, b1, a2, b2, num, den);
cout<<" The sum of the fractions is" << num << "/" << den;
break;
case '-':
subtractFractions(a1,b1, a2, b2, num, den);
cout<<" Difference of the fractions is "<< num << "/" << den;
break;
case '*':
multiplyFractions(a1,b1, a2, b2, num, den);
cout << " Product of the fractions is "<< num << "/" << den;
break;
case '/':if ( a2 != 0 )
{
divideFractions(a1,b1,a2,b2,num, den);
cout <<" Divsion of the fractions = " << num << "/" << den;
}
else
cout <<" Denominator fraction "<< "cannot be zero.";
break;
}
}
catch(int x)
{
cout<<"Denominator is "<<x<<"Exception"<<endl;
}
catch(string s)
{
cout<<s;
}
cout<<endl;
system("pause");
return 0;
}
char menu()
{
char operationType;
cout << " Program that lets the user perform "<< "arithmetic operations on fractions.";
cout << " M E N U";
cout << " Addition :+"
<< " Subtraction :-"
<< " Multiplication :*"
<< " Division :/";
cout << " Enter operation to perform : ";
cin >> operationType;
return operationType;
}
void addFractions( int a1, int b1, int a2, int b2, int& num, int& den )
{
num = ( a1 * b2 ) + ( b1 * a2 );
den = b1 * b2;
}
void subtractFractions( int a1, int b1, int a2, int b2, int& num, int& den )
{
num = ( a1 * b2 ) - ( b1 * a2 );
den = b1 * b2;
}
void multiplyFractions( int a1, int b1, int a2, int b2,int& num, int& den )
{
num = a1 * a2;
den = b1 * b2;
}
void divideFractions( int a1, int b1, int a2, int b2, int& num, int& den )
{
num = a1 * b2;
den = b1 * a2;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.