I don't even know where to start "operation on fraction"

i need help on my coursework
hope someone can help me

My question :

This program performs operations on fraction.
1 : To add fraction
2 : To Subtract fraction
3 : To multiply fraction
4 : To divide fraction
9 : To exit the program

Please Enter Your Choice : 1

For fraction 1
Enter the numerator :
Enter the denominator :

For fraction 2
Enter the numerator :
Enter the denominator :

Result : (fraction1) +(depends on my choice) (fraction2) = (answer)

________________________________________________________________________________

if choice is other than 1,2,3,4,9
then show out 'Invalid Selection'

________________________________________________________________________________

if choice is 9
then show out 'Thanks you for using the FRACTION CALCULATOR program.

________________________________________________________________________________

and 1 of the result i have to print out is :

Please Enter your choice :1

For fraction 1
Enter the numerator: 3
Enter the denominator: 0

the denominator must be nonzero.
Enter the denominator:5

For fraction 2
Enter the numerator: 0
Enter the denominator: 5

Result = (3/5) + (0/5) = 15/25

________________________________________________________________________________



KINDLY thks for your help
Last edited on
You can start with the code you know, at least output the questions as shown on the assignment, get the user input and store it in appropriatly named variables and so on. From there we can help you with the acctual operations since this does "feel" differant from doing math operations for fractions on paper.
#include <iostream>
using namespace std;

int main()
void frac()
{
int num1,num2,deno1,deno2,result;

cout << "For fraction 1" <<endl ;
cout << " Enter the numerator: " ;
cin >> num1;

cout << " Enter the denominator: ";
cin >> deno1;

cout << "For fraction 2" <<endl;
cout << " Enter the numerator: " ;
cin >> num2;

cout << " Enter the denominator: " ;
cin >> deno2;


{

void add()
{
//formula
}

void subtract()
{
//formula
}

void multiple()
{
//formula
}

void divide()
{
//formula
}

int menu()
{
int option;

printf(" 1. To add fraction\n");
printf(" 2. To Subtract fraction\n");
printf(" 3. To Multiple fraction\n");
printf(" 4. To Divide fraction\n");
printf(" 9. Thank you for using the FRACTION CALCULATOR program\n");


cout << "\n Please Enter your choice : ";
cin >> option;

return option;
}

int main()
{
int option;

option = menu();

if(option == 1)
{
add();
}

else if(option == 2)
{
subtract();
}

else if(option == 3)
{
multiple();
}

else if(option == 4)
{
divide();
}

else if(option ==9)
{
exit(0);
}

else
{
cout << "\n invalid selection" <<endl;

}

system("pause");
}


____________________________________________________________________________

i write like that
but i know got very many error inside that
it take my half day to think about it
my head is gonna to boom

i don't know i should write int main(void) OR int main() void frac()
and the formula don't know how to write it
sorry
not prinf but cout <<
#include <iostream>
using namespace std;

int main()
void frac()
{
int num1,num2,deno1,deno2,result;

cout << "For fraction 1" <<endl ;
cout << " Enter the numerator: " ;
cin >> num1;

cout << " Enter the denominator: ";
cin >> deno1;

cout << "For fraction 2" <<endl;
cout << " Enter the numerator: " ;
cin >> num2;

cout << " Enter the denominator: " ;
cin >> deno2;


{

void add()
{
//write ur self
}

void subtract()
{
//write ur self
}

void multiple()
{
//write ur self
}

void divide()
{
//write ur self
}
void exit()
{
cout << "Thank you for using FRACTION CULCULATOR program." << endl ;
}

int menu()
{
int option;

cout << " 1. To add fraction\n" ;
cout << " 2. To Subtract fraction\n");
cout << " 3. To Multiple fraction\n");
cout << " 4. To Divide fraction\n");
cout << " 9. exit\n");


cout << "\n Please Enter your choice : ";
cin >> option;

return option;
}

int main()
{
int option;

option = menu();

if(option == 1)
{
add();
}

else if(option == 2)
{
subtract();
}

else if(option == 3)
{
multiple();
}

else if(option == 4)
{
divide();
}

else if(option == 9)
{
exit(0);
}

else
{
cout << "\n invalid selection" <<endl;

}

system("pause");
}


_____________________________________________________________________________

this my new work
-You shouldn't need to declare the main() function on what would be line 4.

-Each of your math operations will require you to pass the users input as arguments to each function.

-Each of your functions will likley have a return so they should not be of the type 'void'.

-Consider a switch-case operation instead of all of this if then else who-ha. See here: http://www.cplusplus.com/doc/tutorial/control/#switch

-None of your variables are surviving outside of the void frac() function. This would be a good time to practice pointers if you have learned them otherwise you'll need to do the decision making and the output within the frac() function, this isn't wrong per say but rather a little awkward.

-If you are allowed for simplicity sake you may want to try using global variables in this one, in which case you will not have to get into pointers, pass your variables into each function or have your functions return anything.

Ok so I said I would help with the math and here I go:

ADD: You will need to find a common denominator, the easiest way to do this is simply multiply the two denominators together then multiply the numerators by the same amount as their respective denominators. Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//... Code code code

//Assuming variables are global

void Add() {
   N1 = N1 * D2;
   D1 = D1 * D2;

   N2 = N2 * D1;
   D2 = D2 * D1;

// Now Add
   N1 = N1 + N2;

} //End of Add(...) function 


EDIT: Do you need to return the answer as the lowest common denominator? This would be an interesting twist to the assignment.

REEDIT: As for your choice menu, under option 9 replace exit(0); with return 0; and that should be fine. You will also want to tag on a return 0; after system("pause"); I'll skip the obligatory rant as to why you shouldn't ever use the system function for later.
Last edited on
#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 exitFunction();



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 <<"\nFor fraction 1" << endl;

cout << "Enter the numerator: ";

cin >> num1;


cout << "Enter the denominator: ";

cin >> den1;


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

cout << "Enter the numerator: " ;

cin >> num2;

cout << "Enter the denominator: " ;

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:

exitFunction();

default:

cout<<"Invalid Selection"<<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 << "Result : (" << 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 << "Result : (" << 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 << "Result : (" << 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 << "Result : (" << num1 << "/" << den1 << ")" << " / " << "(" << num2 << "/" << den2 << ")" << " = " << numres << "/" << demres << endl;

}

else

cout<<"Denominator must be nonzero."<<endl;

}

void exitFunction()

{

cout << "Thank you for using the FRACTION CALCULATOR program"<< endl;

}

_____________________________________________________________________________

i try redo another one and it complete
but not as done as what i want

1 > when i press the choice as '5' or other than 1,2,3,4,9
it ask me to continue type in the num and den
but what i want is it show out 'Invalid Selection'

2 > when i press the choice as '9'
i want it show me 'Thank you for using the FRACTION CALCULATOR program' only
but there got 'invalid selection'
Topic archived. No new replies allowed.