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 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()
{
cout << ' ' << endl;
system ("pause");
return 0;
}

{
int choice, num1, num2, den1, den2, numres = 0, demres = 0;
cout<<"This program performs operations on fraction. Enter"<<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;
}``````
2
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by baconswife

Make your menuFuntion() return whether or not the user elects to continue. So main would look something like:

``````bool menuFunction();

int main()
{
;
cout << "Goodbye.\n";
return 0;
}``````

Your exit() function is a dangerous name conflict, especially just to write something before quitting. Hence, you can get rid of it as I have above. (Or just rename it to something consistent, like exitFunction() or goodbyeFunction().)

You might also want to check whether the user entered '9' before asking for the numerator and denominator. "I want to quit." --> "Great! What numerator would you like to use?" Not exactly friendly.

Hope this helps.

Edited by Duoas: n/a

I fixed it some what but am still having 2 problems. When I put 9 it outputs goodbye as it should but it also outputs 1 right under goodbye and then under that outputs invalid input. The other problem is that you can type 5 or any other number and it will still run through rather than just saying invalid input

``````#include <iostream>

using namespace std;

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 goodbyeFunction();

int main()
{
cout << ' ' << endl;
system ("pause");
return 0;
}

{

int choice, num1, num2, den1, den2, numres = 0, demres = 0;
while (choice != 9)
{
cout<<"This program performs operations on fraction. Enter"<<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;
if (choice != 9)
{
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;
}
else
cout << "Goodbye." << endl;
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:
cout << goodbyeFunction << endl;
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 goodbyeFunction()
{
cout << "Goodbye."<< endl;
}``````

Fix your indentation and you'll see what is going wrong.
Good luck!

Fixed my indentation but for some reason still having trouble figuring out where I'm wrong. I think it may be because I have been staring at it for so long

``````#include <iostream>

using namespace std;

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 goodbyeFunction();

int main()
{
cout << ' ' << endl;
system ("pause");
return 0;
}

{

int choice, num1, num2, den1, den2, numres = 0, demres = 0;
while (choice != 9)
{
cout<<"This program performs operations on fraction. Enter"<<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;
if (choice != 9)
{
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;
}
else
cout << "Goodbye." << endl;
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:
cout << goodbyeFunction << endl;
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 goodbyeFunction()
{
cout << "Goodbye."<< endl;
}``````

I got it to leave out the one but am still having trouble with the invalid input part

``````#include <iostream>

using namespace std;

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 goodbyeFunction();

int main()
{
cout << ' ' << endl;
system ("pause");
return 0;
}

{

int choice, num1, num2, den1, den2, numres = 0, demres = 0;
while (choice != 9)
{
cout<<"This program performs operations on fraction. Enter"<<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;
if (choice != 9)
{
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:
goodbyeFunction();
default:
cout<<"Invalid input."<<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 goodbyeFunction()
{
cout << "Goodbye."<< endl;
}``````

Wow, shows how much a break statement helps. Thank you!