# Thread: Help with dividing fractions

1. ## Help with dividing fractions

Hello,
I have been trying to write a program that takes two fractions entered by the user and divides them. This is the code I have but unfortunatly even with a simple division of 1/2 by 1/2 gives me 1232938213982139. Something is not right and I cannot figure it out. Any suggestions would be great.

Code:
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
int num1;
int den1;
int num2;
int den2;
int fnlnum;
int fnlden;

void divideFractions(int num1, int num2, int den1, int den2);

{
fnlnum= num1 * den2;
fnlden= den1 * num2;
}

{

cout << "Dividing Fractions Calculator" << endl;

cin >> num1;

cin >> den1;

cin >> num2;

cin >> den2;

cout << num1 << "/" << den1 << " divided by " << num2
<< "/" << den2 << "=" << fnlnum << "/" << fnlden << endl;

if (den1 == 0 || den2 == 0)
{
cout << "You must enter a number other than 0" << endl;

}

}
system("PAUSE");
return EXIT_SUCCESS;
}

2. Originally Posted by limburger
Hello,
I have been trying to write a program that takes two fractions entered by the user and divides them. This is the code I have but unfortunatly even with a simple division of 1/2 by 1/2 gives me 1232938213982139. Something is not right and I cannot figure it out. Any suggestions would be great.

Code:
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
int num1;
int den1;
int num2;
int den2;
int fnlnum;
int fnlden;

void divideFractions(int num1, int num2, int den1, int den2);

{
fnlnum= num1 * den2;
fnlden= den1 * num2;
}

{

cout << "Dividing Fractions Calculator" << endl;

cin >> num1;

cin >> den1;

cin >> num2;

cin >> den2;

cout << num1 << "/" << den1 << " divided by " << num2
<< "/" << den2 << "=" << fnlnum << "/" << fnlden << endl;

if (den1 == 0 || den2 == 0)
{
cout << "You must enter a number other than 0" << endl;

}

}
system("PAUSE");
return EXIT_SUCCESS;
}
You never initialise or set "fnlden" or "fnlnum" because you never call "divideFractions()". Not sure why you want "divideFractions()" at local scope, that's a poor way to do it. Just remove the funciton, and do the calculation yourself for now. That is, "fnlnum= num1 * den2;", etc. After you have read num1, num2, den1 and den2.
You also have a few extra brackets, and extra semi-colon (highlighted above).

3. Thanks that helped a lot. I have only been doing this for a couple weeks now and I am slowly learning.