Here is my code including header files.
My program is supposed to be a fraction calculator that handles exceptions such as division by zero, division by non-integer values, allows user to re-enter any values the program threw an exception for, and allows the user to continue to use the program until they quit via a menu option.
I have it all working except that the program can't tell the difference between the 'x' and 'X' characters and any other non-integer characters. In my if statements where it states a difference between these two types of characters, my program keeps treating all non-integer characters as if they were x or X rather than throwing the exception specifically for non-integers. (I bolded this area for easy reference.)
Any advice would be greatly appreciated!
Thanks!
Erin0201
#include <iostream>
#include <exception>
#include <string>
#include "divisionByZero.h"
#include "exitNow.h"
#include "keepGoing.h"
#include <cstdlib>
using namespace std;
void menu(); //main menu
void addFractions(int numer1, int numer2, int denom1, int denom2);
//function to add fractions
void subtractFractions(int numer1, int numer2, int denom1, int denom2);
//function to subtract fractions
void multiplyFractions(int numer1, int numer2, int denom1, int denom2);
//function to multiply fractions
void divideFractions(int numer1, int numer2, int denom1, int denom2);
//function to divide fractions
int main()
{
menu();
return 0;
}
void menu()
{
int num1; //variable to hold first fraction numerator
int num2; //variable to hold second fraction numerator
int den1; //variable to hold first fraction denominator
int den2; //variable to hold second fraction denominator
char operation = 'c'; //holds input for operators
bool done = false; //stops do while loop when set to true
do
{
cout << "Welcome to the fraction calculator." << endl;
cout << endl;
cout << "Enter x to exit the program early." << endl;
cout << "Please enter a numerator and a denominator for each fraction. ";
cin >> num1 >> den1 >> num2 >> den2;
cout << endl;
try
{
[B]if (!cin)
{
if (num1 || num2 || den1 || den2 == 'x' || 'X')
{
done = true;
throw exitNow();
}
else
{
throw keepGoing();
}
} [/B]
if (den1 == 0)
throw divisionByZero();
else if (den2 == 0)
throw divisionByZero();
cout << "You entered..." << endl;
cout << "First Fraction: " << num1 << "/" << den1 << endl;
cout << "Second Fraction: " << num2 << "/" << den2 << endl;
cout << endl;
cout << "Please enter your desired operation for the fractions (+, -, /, *) " << endl;
cout << "or enter x to return to the main menu: ";
cin >> operation;
cout << endl;
switch (operation) //call operator function based on case input
{
case '+':
addFractions(num1, num2, den1, den2);
break;
case '-':
subtractFractions(num1, num2, den1, den2);
break;
case '*':
multiplyFractions(num1, num2, den1, den2);
break;
case '/':
divideFractions(num1, num2, den1, den2);
break;
default:
cout << "Returning to main menu.."<< endl;
cout << endl;
cout << endl;
cout << "**************************************" << endl;
cout << endl;
cout << endl;
}
}
catch (keepGoing keepGoingObj) //notice non-integers, clear input, continue
{
cout << "Clearing input.." << endl;
cin.clear();
cin.ignore(100, '\n');
cout << endl;
cout << endl;
cout << "*************************************" << endl;
cout << endl;
cout << endl;
}
catch (divisionByZero divByZeroObj) //notice 0 input, clear input, continue
{
cout << divByZeroObj.what() << endl;
cout << "Clearing input.." << endl;
cin.clear();
cin.ignore(100, '\n');
cout << endl;
cout << endl;
cout << "**************************************" << endl;
cout << endl;
cout << endl;
}
catch (exitNow exitNowObj) //notice x input, exit program
{
cout << exitNowObj.what() << endl;
cout << "Exiting......." << endl;
exit(1);
}
}
while (!done);
}
//function to add fractions
void addFractions(int numer1, int numer2, int denom1, int denom2)
{
int num1 = numer1;
int num2 = numer2;
int den1 = denom1;
int den2 = denom2;
int num, den;
num = ((num1*den2) + (num2*den1));
den = (den1*den2);
cout << "The answer is " << num << "/" << den << endl;
cout << endl;
cout << endl;
cout << "**************************************" << endl;
cout << endl;
cout << endl;
}
//function to subtract fractions
void subtractFractions(int numer1, int numer2, int denom1, int denom2)
{
int num1 = numer1;
int num2 = numer2;
int den1 = denom1;
int den2 = denom2;
int num, den;
num = ((num1*den2) - (num2*den1));
den = (den1*den2);
cout << "The answer is " << num << "/" << den << endl;
cout << endl;
cout << endl;
cout << "**************************************" << endl;
cout << endl;
cout << endl;
}
//function to multiply fractions
void multiplyFractions(int numer1, int numer2, int denom1, int denom2)
{
int num1 = numer1;
int num2 = numer2;
int den1 = denom1;
int den2 = denom2;
int num, den;
num = (num1*num2);
den = (den1*den2);
cout << "The answer is " << num << "/" << den << endl;
cout << endl;
cout << endl;
cout << "**************************************" << endl;
cout << endl;
cout << endl;
}
//function to divide fractions
void divideFractions(int numer1, int numer2, int denom1, int denom2)
{
int num1 = numer1;
int num2 = numer2;
int den1 = denom1;
int den2 = denom2;
int num, den;
num = (num1*den2);
den = (den1*num2);
cout << "The answer is " << num << "/" << den << endl;
cout << endl;
cout << endl;
cout << "**************************************" << endl;
cout << endl;
cout << endl;
}
//Class to handle program exit.
#include <iostream>
#include <string>
using namespace std;
class exitNow
{
public:
exitNow()
{
message = "Exiting program early....";
}
exitNow(string str)
{
message = str;
}
string what()
{
return message;
}
private:
string message;
};
//Class to handle continuation after entering non-integer.
#include <iostream>
#include <string>
using namespace std;
class keepGoing
{
public:
keepGoing()
{
message = "Input invalid. Only an integer is allowed.";
}
keepGoing(string str)
{
message = str;
}
string what()
{
return message;
}
private:
string message;
};
//Class to handle division by zero
#include <iostream>
#include <string>
using namespace std;
class divisionByZero
{
public:
divisionByZero()
{
message = "Division by zero";
}
divisionByZero(string str)
{
message = str;
}
string what()
{
return message;
}
private:
string message;
};