# Help with dividing fractions

• 03-02-2010
limburger
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;                    cout << "Please enter your first numerator:" << endl;                cin >> num1;                                                        cout << "Please enter your first denominator:" << endl;              cin >> den1;                                                          cout << "Please enter your second numerator:" << endl;                  cin >> num2;                                                        cout << "Please enter your second denominator:" << endl;              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; }```
• 03-02-2010
zacs7
Quote:

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;                    cout << "Please enter your first numerator:" << endl;                cin >> num1;                                                        cout << "Please enter your first denominator:" << endl;              cin >> den1;                                                          cout << "Please enter your second numerator:" << endl;                  cin >> num2;                                                        cout << "Please enter your second denominator:" << endl;              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).
• 03-02-2010
limburger
Thanks that helped a lot. I have only been doing this for a couple weeks now and I am slowly learning.