Hi i was wondering if someone could tell if i am anywhere close to being on the right track.

I have to create a program which adds, subtracts, multiplies, and divides fractions.
I really am trying to get a grip on learning this stuff, so anyone who could give some input would be greatly appretiated.

``````#include <iostream>
#include <cmath>

using namespace std;

void showRules();

void functsub(num1, denom1, num2, denom2);

void functmult(num1, denom1, num2, denom2);

void functdivi(num1, denom1, num2, denom2);

//main function
int main()
{
int num1, denom1, num2, denom2;
char op;
//show rules
showRules();

cin>>op;

switch(op)
{
case '+':
break;

case '-':
// User entered a subtraction sign, subtract the fractions
functsub(num1, denom1, num2, denom2);
break;

case '*':
// User entered a multiplication sign, multiply the fractions
functmult(num1, denom1, num2, denom2);
break;

case '/':
// User entered a division sign, divide the fractions
functdivi(num1, denom1, num2, denom2);
break;

default:
// User entered something else, show an error message and don't
cout << "You have used an invalid operator.\n";
cout << "Please use any one of following operators: +, -, *, /\n";
showans = false;
break;
}

return 0;
}
{
char ch;
cin>>n;
cin>>ch;
cin>>d;
}

//shows user the rules
void showRules()
{
cout>>"Fractional Arithmetic Program\n">>endl;
cout>>"This program will perform arithmetic on fractions.">>endl;
cout>>"Problems should be entered like this: 2/5 - 4/7\n">>endl;
cout>>"The allowed operations are addition (+), subtraction (-),">>endl;
cout>>"mulitplication (*), and division (/).\n">>endl;
return;
}

void functadd(int& numb1, int& denoma1, int& numb2, int& denoma2)
{
int ansnum, asdenom;

ansnum =(numb1*denom2) + (num2/denoma1);
ansdenom = denoma1 * denoma2;

cout<< ansnum << "/" << ansdenom <<endl;
}

//subtract funct
void functsub(int& numb1, int& denoma1, int& numb2, int& denoma2)
{
int ansnum, asdenom;
ansnum =(numb1*denoma2) - (numb2/denoma1);
ansdenom = denom1 * denom2;
cout<< ansnum << "/" << ansdenom <<endl;

}

//multiply function
void functmult(int& numb1, int& denoma1, int& numb2, int& denoma2)
{
int ansnum, asdenom;

ansnum = numb1*numb2;
ansdenom = denoma1 * denoma2;

cout<< ansnum << "/" << ansdenom <<endl;
}

//divide fucntion
void functdivi(int& numb1, int& denoma1, int& numb2, int& denoma2)
{
int ansnum, asdenom;

ansnum =numb1 * denoma2;
ansdenom = denoma1 * numb2;

cout<< ansnum << "/" << ansdenom <<endl;
}``````

You need to declare readFrac() before you call it, but otherwise it looks pretty good. Does it compile? run? give an expected answer when known input is presented? If so, then you've done your job. You could spice it up using a class to define a type called something like RationalNumber or Fractions or something. You could add functionality to put the answers into standard form with lowest common denominators (eg 6/8 becomes 3/4, etc). But those are bells and whistles. Your code does the basics, or at least it looks like it does.

Be a part of the DaniWeb community

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