Thread: Help with dividing fractions

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    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;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by limburger View Post
    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).
    Last edited by zacs7; 03-02-2010 at 04:31 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Thanks that helped a lot. I have only been doing this for a couple weeks now and I am slowly learning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 05:10 AM
  3. Dividing fractions help!!!
    By Staractor6 in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 11:42 AM
  4. Dividing int numbers by 256, but fast!
    By Boksha in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 03:44 PM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM