Hello, for some reason I am having trouble remembering how to make it exit when they push the number 9. Can anyone help me? Also, I need it to repeat rather than at the end saying "press any key to continue" I know I will do this using while (choice != 9) but am not sure where I should place it in the program. If anyone has any ideas please let me know.
#include <iostream>
using namespace std;
void menuFunction();
void addFrac(int num1, int num2, int den1, int den2, int numres, int demres);
void subtractFraction(int num1, int num2, int den1, int den2, int numres, int demres);
void multiplyFraction(int num1, int num2, int den1, int den2, int numres, int demres);
void divideFraction(int num1, int num2, int den1, int den2, int numres, int demres);
void exit();
int main()
{
menuFunction();
cout << ' ' << endl;
system ("pause");
return 0;
}
void menuFunction()
{
int choice, num1, num2, den1, den2, numres = 0, demres = 0;
cout<<"This program performs operations on fraction. Enter"<<endl;
cout<<"1 : To add fraction"<<endl;
cout<<"2 : To subtract fraction"<<endl;
cout<<"3 : To multiply fraction"<<endl;
cout<<"4 : To divide fraction"<<endl;
cout<<"9 : To exit the program"<<endl;
cin >> choice;
cout <<"For fraction 1" << endl;
cout << "Enter the numerator: " << endl;
cin >> num1;
cout<<" " << endl;
cout << "Enter the denominator: " << endl;
cin >> den1;
cout<<" " << endl;
cout<<" " << endl;
cout <<"For fraction 2" << endl;
cout << "Enter the numerator: " << endl;
cin >> num2;
cout<<" " << endl;
cout << "Enter the denominator: " << endl;
cin >> den2;
switch(choice)
{
case 1:
addFrac(num1, num2, den1, den2, numres, demres);
break;
case 2:
subtractFraction(num1, num2, den1, den2, numres, demres);
break;
case 3:
multiplyFraction(num1, num2, den1, den2, numres, demres);
break;
case 4:
divideFraction(num1, num2, den1, den2, numres, demres);
break;
case 9:
exit();
break;
default:
cout<<"\nInvalid input.\n"<<endl;
}
}
void addFrac(int num1, int num2, int den1, int den2, int numres, int demres)
{
if(den1 != 0 && den2 != 0)
{
numres = ((num1 * den2) + (num2 * den1));
demres = num2 * den2;
cout << num1 << "/" << den1 << " " << "+" << " " << num2 << "/" << den2 << "=" << numres << "/" << demres << endl;
}
else
cout<<"Denominator must be nonzero."<<endl;
}
void subtractFraction(int num1, int num2, int den1, int den2, int numres, int demres)
{
if(den1 != 0 && den2 != 0)
{
numres = ((num1 * den2) - (num2 * den1));
demres = num2 * den2;
cout << num1 << "/" << den1 << " " << "-" << " " << num2 << "/" << den2 << "=" << numres << "/" << demres << endl;
}
else
cout<<"Denominator must be nonzero."<<endl;
}
void multiplyFraction(int num1, int num2, int den1, int den2, int numres, int demres)
{
if(den1 != 0 && den2 != 0)
{
numres = num1 * num2;
demres = den1 * den2;
cout << num1 << "/" << den1 << " " << "*" << " " << num2 << "/" << den2 << "=" << numres << "/" << demres << endl;
}
else
cout<<"Denominator must be nonzero."<<endl;
}
void divideFraction(int num1, int num2, int den1, int den2, int numres, int demres)
{
if(den1 != 0 && den2 != 0)
{
numres = num1 * den2;
demres = num2 * den1;
cout << num1 << "/" << den1 << " " << "/" << " " << num2 << "/" << den2 << "=" << numres << "/" << demres << endl;
}
else
cout<<"Denominator must be nonzero."<<endl;
}
void exit()
{
cout << "Goodbye." << endl;
}