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;                           
};

cin won't allow alpha characters to be entered into numeric fields. So when you have cin >> num1 num1 will NEVER have 'x' or 'X'.

What I would do is get input data as strings so that you can easily determine if the input character is 'X'. Something like this:

std::string input1, input2;
cout << "Enter x to exit the program early." << endl;
cout << "Please enter a numerator and a denominator for each fraction. ";
cin >> input1 >> input2;
if( input1.length() > 0 && input2.length() > 0)
{
    if( input1[0] == 'X' || input2[0] == 'X')
    {

     }
    // validate input is all numeric digits
    //
    //
    // convert to integers
    num1 = atol(input1.c_str());
    num2 = atol(input2.c_str());    
    // blabla

}

Thanks a lot for that suggestion Ancient Dragon. I was actually considering trying that last night, but it was reaching 1am and I gave up for the night. ;) I will definitely see what I can do with using strings instead.

Thanks!
Erin0201

Ok, this is what I have so far. Now I've got all sorts of issues. It will exit the program when I type x now, but now if it finds an integer it clears the input and then makes the user re-enter all of the integers rather than starting over at each individual line. At that point it starts seeing integers as invalid and continuously stays on the first input and keeps clearing the input stream each time you enter an integer.

I'm also getting huge numbers for my input of 1 or 2 which messes up my calculations.

Any help would be greatly appreciated. I feel like I was closer with the other program, but I know I needed to use strings. Now I'm just a little frustrated because I can't figure out how to get things working from here and I've been at this for 6 hours. :(

Thanks for any advice or help! Its really appreciated!
Erin0201

#include <iostream>
#include <exception>
#include <string>
#include "divisionByZero.h"
#include "exitNow.h"
#include "keepGoing.h"
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <cctype>

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
void checkForZero(string numer1, string numer2, string denom1, string denom2);
void checkForInt(string numer1, string numer2, string denom1, string denom2);
void checkForX(string numer1, string numer2, string denom1, string denom2);
void chooseOperation(int numer1, int numer2, int denom1, int denom2);
//void convertStrtoInt(string numer1, string numer2, string denom1, string denom2);


int main()
	{
	  
	menu();

	return 0;
	}

void menu()
	{
		
		char operation = 'c'; 	//holds input for operators
	    bool done = false;		//stops do while loop when set to true
		string numer1;			//holds first numerator
		string numer2;			//holds second numerator
		string denom1;		   //holds first denominator
		string denom2;			//holds second denominator

		do
			{
				

				try
					{

						string n1 = numer1;
						string n2 = numer2;
						string d1 = denom1;
						string d2 = denom2;
						int num1, num2, den1, den2;
						int len1 = n1.length();
						int len2 = n2.length();
						int len3 = d1.length();
						int len4 = d2.length();
						char chn1[10];
						char chn2[10];
						char chd1[10];
						char chd2[10];
						
						cout << "Welcome to the fraction calculator." << endl;
						cout << endl;
						cout << "Enter x to exit the program early." << endl;
						cout << endl;
						
						cout << "Enter the numerator for the first fraction: ";
						cin >> numer1;
						checkForX(numer1, numer2, denom1, denom2);
						checkForZero(numer1, numer2, denom1, denom2);
						checkForInt(numer1, numer2, denom1, denom2);
					

						for (int i = 0; i < len1; i++)
							{
							 chn1[i] = n1[i];		 

								while(chn1[i] != '\0') 
								{
									if(isdigit(chn1[i]))
									{
									num1 = atoi (chn1);
									break;
									}
								}
							}
												

						cout << "Enter the denominator for the first fraction: ";
						cin >> denom1;
						checkForX(numer1, numer2, denom1, denom2);
						checkForZero(numer1, numer2, denom1, denom2);
						checkForInt(numer1, numer2, denom1, denom2);
						for (int i = 0; i < len3; i++)
							{
							 chd1[i] = d1[i];		 

								while(chd1[i] != '\0') 
								{
									if(isdigit(chd1[i]))
									{
									den1 = atoi (chd1);
									break;
									}
								}
							}

						cout << "Enter the numerator for the second fraction: ";
						cin >> numer2;
						checkForX(numer1, numer2, denom1, denom2);
						checkForZero(numer1, numer2, denom1, denom2);
						checkForInt(numer1, numer2, denom1, denom2);
						for (int i = 0; i < len2; i++)
							{
							 chn2[i] = n2[i];		 

								while(chn2[i] != '\0') 
								{
									if(isdigit(chn2[i]))
									{
									num2 = atoi (chn2);
									break;
									}
								}
							}
							 	

						cout << "Enter the denominator for the second fraction: ";
						cin >> denom2;					
						checkForX(numer1, numer2, denom1, denom2);
						checkForZero(numer1, numer2, denom1, denom2);
						checkForInt(numer1, numer2, denom1, denom2);
						for (int i = 0; i < len4; i++)
							{
							 chd2[i] = d2[i];		 

								while(chd2[i] != '\0') 
								{
									if(isdigit(chd2[i]))
									{
									den2 = atoi (chd2);
									break;
									}
								}
							}
						chooseOperation(num1, num2, den1, den2);
											
					}
				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;
					done = true;
					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;
}

void checkForZero(string numer1, string numer2, string denom1, string denom2)
	{
	string num1 = numer1;
	string num2 = numer2;
	string den1 = denom1;
	string den2 = denom2; 

	try
		{
		if(num1[0] == '0' || den1[0] == '0' || num2[0] == '0' || den2[0] == '0')
		{
		cout << "Cannot divide with 0!" << endl;
		throw keepGoing();
		}
		}
	catch (keepGoing)
		{
		throw;
		}
	}

void checkForInt(string numer1, string numer2, string denom1, string denom2)
	{
	string n1 = numer1;
	string n2 = numer2;
	string d1 = denom1;
	string d2 = denom2;
//	int num1, num2, den1, den2;
	int len1 = n1.length();
	int len2 = n2.length();
	int len3 = d1.length();
	int len4 = d2.length();
	char chn1[20];
	char chn2[20];
	char chd1[20];
	char chd2[20];

	try
		{
		for (int i = 0; i < len1; i++)
			{
			 chn1[i] = n1[i];		 

			while(chn1[i] != '\0') 
				{
				if(isalpha(chn1[i]))
					{
					cout << "Invalid input.  Please input an integer." << endl;
					throw keepGoing();
					}
				else
					break;
				}
			
			}
		for (int i = 0; i < len2; i++)
		{
		 chn2[i] = n2[i]; 		 

		while(chn2[i] != '\0') 
		{
			if(isalpha(chn2[i]))
				{
				cout << "Invalid input.  Please input an integer." << endl;
				throw keepGoing();
				}
			else
				break;
			}
		}

		for (int i = 0; i < len3; i++)
		{
		 chd1[i] = d1[i]; 		 

		while(chd1[i] != '\0') 
		{
			if(isalpha(chd1[i]))
				{
				cout << "Invalid input.  Please input an integer." << endl;
				throw keepGoing();
				}
			else
				break;
			}
		}

		for (int i = 0; i < len4; i++)
		{
		 chd2[i] = d2[i];  		 

		while(chd2[i] != '\0') 
		{
			if(isalpha(chd2[i]))
				{
				cout << "Invalid input.  Please input an integer." << endl;
				throw keepGoing();
				}
			else
				break;
			
			}
			}
		}
			
		
	catch (keepGoing)
		{
		throw;
		}
}

void checkForX(string numer1, string numer2, string denom1, string denom2)
	{
	string num1 = numer1;
	string num2 = numer2;
	string den1 = denom1;
	string den2 = denom2; 

 try
	 {
	if(num1[0] == 'x' || den1[0] == 'x' || num2[0] == 'x' || den2[0] == 'x')
		{
		cout << "You have entered x.  Exiting the program...  Goodbye!" << endl;
		throw exitNow();
		}
	 }
	catch (exitNow)
		{
		throw;
		}
	}

/*void convertStrtoInt(string numer1, string numer2, string denom1, string denom2)
	{
	string n1 = numer1;
	string n2 = numer2;
	string d1 = denom1;
	string d2 = denom2; 
	int num1, num2, den1, den2;
	int len1 = n1.length();
	int len2 = n2.length();
	int len3 = d1.length();
	int len4 = d2.length();
	char chn1[20];
	char chn2[20];
	char chd1[20];
	char chd2[20];
	
	try
		{
	for (int i = 0; i < len1; i++)
		{
		 chn1[i] = n1[i];		 

		while(chn1[i] != '\0') 
		{
			if(isalpha(chn1[i]))
				{
				cout << "Invalid input.  Please input an integer." << endl;
				throw keepGoing();
				}

			if(isdigit(chn1[i]))
				{
				num1 = atoi (chn1);
				}  
			}
			
			}
		for (int i = 0; i < len2; i++)
		{
		 chn2[i] = n2[i]; 		 

		while(chn2[i] != '\0') 
		{
			if(isalpha(chn2[i]))
				{
				cout << "Invalid input.  Please input an integer." << endl;
				throw keepGoing();
				}

			if(isdigit(chn2[i]))
				{
				num2 = atoi (chn2);
				}  
			}
		}

		for (int i = 0; i < len3; i++)
		{
		 chd1[i] = d1[i]; 		 

		while(chd1[i] != '\0') 
		{
			if(isalpha(chd1[i]))
				{
				cout << "Invalid input.  Please input an integer." << endl;
				throw keepGoing();
				}

			if(isdigit(chd1[i]))
				{
				den1 = atoi (chd1);
				}	 
			}
		}

		for (int i = 0; i < len4; i++)
		{
		 chd2[i] = d2[i];  		 

		while(chd2[i] != '\0') 
		{
			if(isalpha(chd2[i]))
				{
				cout << "Invalid input.  Please input an integer." << endl;
				throw keepGoing();
				}

			if(isdigit(chd2[i]))
				{
				den2 = atoi (chd2);
				}	
			}
			}
	chooseOperation(num1, num2, den1, den2);
		}
	catch (keepGoing)
		{
		throw;
		}

	}   */



void chooseOperation(int numer1, int numer2, int denom1, int denom2)
	{
	int num1 = numer1;
	int num2 = numer2;
	int den1 = denom1;
	int den2 = denom2;
	char operation = 'c';
	//int num, den;

	cout << endl;
	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;
								
			}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.