converting answer from a fraction to a decimal

hey guys, i figured out how to do this problem that my professor wanted but he put a twist on it that i cant figure out how to solve. he wants the final answer to display a decimal number and not a fraction. this is what i have, any help would be great, thanks.

#include <iostream>

using namespace std;


void addFraction(int, int, int, int, int&, int&);
void subtractFraction(int, int, int, int, int&, int&);
void multiplyFraction(int, int, int, int, int&, int&);
void divideFraction(int, int, int, int, int&, int&);
void startFunction();
int main()
{
startFunction();
return 0;
}

void startFunction()
{
int choice;
int num1, num2, denom1, denom2, numResult = 0, denomResult = 0;

cout<<"Enter the first Numerator: ";
cin>>num1;
cout<<"Enter the first Denominator: ";
cin>>denom1;
cout<<"Enter the second Numerator: ";
cin>>num2;
cout<<"Enter the second denominator: ";
cin>>denom2;
cout<<"Enter operation number (1= Add, 2= Subtract, 3= Multiply, 4= Divide): "<<endl;
cin>>choice;
cout<<endl;



switch(choice)
{
case 1:
addFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 2:
subtractFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 3:
multiplyFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 4:
divideFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
default:
cout<<"\nInvalid input.\n"<<endl;
}
cout<<"Result = "<<numResult<<"/"<<denomResult<<endl;
}

void addFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int numerator1 = (bottom1 * bottom2) / bottom1;
int numerator2 = (bottom1 * bottom2) / bottom2;
bottomResult = bottom1 * bottom2;
int newNum1 = numerator1 * top1;
int newNum2 = numerator2 * top2;
topResult = newNum1 + newNum2;

}
else
cout<<"\nInvalid input, denominator cannot be 0."<<endl;

}

void subtractFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int numerator1 = (bottom1 * bottom2) / bottom1;
int numerator2 = (bottom1 * bottom2) / bottom2;
bottomResult = bottom1 * bottom2;
int newNum1 = numerator1 * top1;
int newNum2 = numerator2 * top2;
topResult = newNum1 - newNum2;

}
else
cout<<"\nInvalid input, denominator cannot be 0."<<endl;

}

void multiplyFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
bottomResult = bottom1 * bottom2;
topResult = top1 * top2;

}
else
cout<<"\nInvalid input, denominator cannot be 0."<<endl;

}

void divideFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int newTop = bottom2;
int newBottom = top2;
topResult = top1 * newTop;
bottomResult = bottom1 * newBottom;

}
else
cout<<"\nInvalid input.\n"<<endl;

}
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

double decimal = 1.0 * numerator / denominator;
Note the 1.0. This will promote numerator to a double, resulting in the result being a double.

You could also do it like this:
double decimal = static_cast<double>( numerator ) / denominator;
But I find it long-winded, for something like this.
Topic archived. No new replies allowed.