code done but still got abit problem

Pages: 12

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



int main()

{

menuFunction();

cout << ' ' << endl;

system ("pause");

return 0;

}



void menuFunction()

{



int choice, num1, num2, den1, den2, numres = 0, demres = 0;

while (choice != 9)

{

cout<<"This program performs operations on fraction."<<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;

cout<<"\nPlease Enter Your Choice: " ;
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 << "Thank you for using the FRACTION CALCULATOR program"<< endl;

}

________________________________________________________________________________

anyone can try for me??
when i type in '5' but it still continue to ask me type in fraction.
but if 'choice != 1,2,3,4,9'... i have to show out 'Invalid Selection'
and when my choice is '9'..it show me what i want but got the 'Invalid Selection' there
can help me solve the problem??
Lol! I love this damn site! I just helped one of your classmates not even two minutes ago. Here you go:

http://www.cplusplus.com/forum/beginner/32248/

- I'm having trouble understanding you but I think you're saying that it doesn't except '5' as an input? Is that the case?
not classmates lah
that me also
HAHAHAHA
this my another new work
xDD
Computergeek01
do u got MSN??
mind that i add u and we chat in MSN? =]
Unfortunatly I'm at work, I kind of do this to keep my brain going during the slow periods of the day.
haha
then sorry for disturb
thks your yr help
u helped me alot
^.^v
-I understand your problem now! you don't have a break; in case 9 before you declare your default case.

-You realize you have both a while(choice != 9) {... and a if(choice != 9) {... a few line apart, this looks redundent.
Last edited on
oo i see...

i got 2 more problem in this code

my question were ask me to show out
____________________________________________________________________________

1 > Please Enter Your Choice : 5 (other than 1,2,3,4,9)
Invalid Selection

2 > For fraction 1
Enter the numerator : 3
Enter the denominator : 0
The denominator must be nonzero.
Enter the denominator :

_____________________________________________________________________________
but when i choose the choice as '5'

it will ask me to type in fraction1 and fraction2 then show me the 'Invalid Selection'
how can i make it show me 'Invalid Selection' only if i type the choice other than '1,2,3,4,9'??

and same case with the 'The denominator must be nonzero'...and how to make it re-ask me to type in again the denominator
Right because the if(choice != 9) {... only tests for that one condition, that the choice entered is not '9', then continues to ask for the input of the fraction, if you enter the info by the time you hit the switch statement your code will operate as you expect.

You just have to change what you are evaluating at this point in the code. Try something like:

1
2
3
4
5
6
7
8
//... Code code code

if(choice == 9)
{
   goodbyeFunction();
}
//... Code code code


And drop the if(choice != 9) {... from the code.
Last edited on
thks for your help
how then can i stop the calculation after i done a calculate?? but not using 'case 9' only??
Something like:

1
2
3
4
5
6
//...code code code

cout << "\nWould you like to continue? Enter 9 to quit: ";
cin >> choice;

//...code code code 


After the closing bracket of your switch(choice) {...} would allow the user to stop the program see his data and decide if he whishes to continue.
it work
but it stuck with the choice if i choose '9'

it will show me "Thank you for using the FRACTION CALCULATOR program"
and then "Would you like to continue? Enter 9 to quit: "

it look not so nice
You put that code after the close bracket to the switch(choice) right?
yah

but i hope it will only show me "Thank you for using the FRACTION CALCULATOR program" and then close if case '9'
It should. When the user enters 9 as a choice at the "Would you like to continue..." part of the code it should go back to the while(choice !=9) and see that choie IS equal to 9 and quit. Unless you are getting stuck at a new loop. Could you copy paste your current code in the void menuFunction() function?

EDIT: I meant to ask for your current version of your void menuFunction not your main().
Last edited on
dude
i done it
thks for your help
void menuFunction()

{



int choice, num1, num2, den1, den2, numres = 0, demres = 0;

{

cout<<"This program performs operations on fraction."<<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;

cout<<"\nPlease Enter Your Choice: " ;
cin >> choice;


if (choice == 1 && 2 && 3 && 4)
{
cout <<"\nFor fraction 1" << endl;

cout << "Enter the numerator: ";
cin >> num1;


cout << "Enter the denominator: ";
cin >> den1;
if (den1 ==0)
{
cout << "Denominator must be nonzero\n";
cout << "Enter the denominator: ";
cin >> den1;
}

cout <<"\nFor fraction 2" << endl;

cout << "Enter the numerator: " ;
cin >> num2;

cout << "Enter the denominator: " ;
cin >> den2;
if (den2 ==0)
{
cout << "Denominator must be nonzero\n";
cout << "Enter the denominator: ";
cin >> den2;
}

}
else if (choice == 9)
{
exitFunction();
}

else
cout << "Invalid Selection";
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:

// exitFunction();

break;

}
}
cout << "\nEnter 9 to quit: ";
cin >> choice;
}

i so confuse now
wait later i send u the latest 1
Wait, I stopped responding because you said you did it. I thought you posted the code as a proff I didn't even read it. Is there another error?
Pages: 12