Thread: help with fraction division function

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    14

    help with fraction division function

    i am working on a fraction calculator using functions and i'm stuck. when i compile my code i get an error that says function fracDiv cannot take 0 parameters... can anyone help? here's my code so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    //Function prototype: fracDiv
    //--------------------------------------------------------------------
    // Function:       fracDiv
    // Purpose:        Divide two fractions and receive the output as a fraction
    //   a/b / c/d = (a*d) / (b*c)
    // Preconditions:  parameters: a (int, numerator of first fraction, value parameter)
    //   b (int, denominator of first fraction, value parameter)
    //   c (int, numerator of second fraction, value parameter)
    //   d (int, denominator of second fraction, value parameter)
    //   num (int, numerator of result, reference parameter)
    //   den (int, denominator of result, reference parameter)
    //  
    // Postconditions: Returns: int--0 if calculation can be performed,
    // 1 if denominator of first fraction is zero
    // 2 if denominator of second fraction is zero
    // 3 if numerator of second fraction is zero
    //   num contains numerator of result 
    //   den contains denominator of result
    //     
    int fracDiv(int a, int b, int c, int d, int& num, int& den);
    
    
    int main()
    {
    	int numerator, denominator;
    	int a = 0;
    	int b = 0;
    	int c = 0;
    	int d = 0;
    	
    	cout << "Enter two fractions: " << endl;
    	cin >> a >> b >> c >> d;
    	
    	fracDiv(numerator, denominator);
    
    	
    	cout << "Answer = " << numerator << "/" << denominator << endl;
    	
    	
    	
    	return 0;
    }
    
    
    //Function
    int fracDiv(int a, int b, int c, int d, int& num, int& den)
    {
    
    
    	num = a * d;
    	den = b * c;
    
    	
    	return 0;
    	
    }

  2. #2
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    move:
    Code:
    int fracDiv(int a, int b, int c, int d, int& num, int& den)
    {
    
    
    	num = a * d;
    	den = b * c;
    
    	
    	return 0;
    	
    }
    to the area above:
    Code:
    int fracDiv(int a, int b, int c, int d, int& num, int& den);
    and see if that helps.
    To code is divine

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You should call your function like this:
    Code:
    fracDiv(a,b,c,d,numerator, denominator);
    Quote Originally Posted by 7smurfs
    move:
    to the area above:
    and see if that helps.
    How on earth would that change anything?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    I thought you couldn't call a function before it was made?
    To code is divine

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes you can, that's the purpose of the declaration of the function name at the beginning.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    Call me dumb, but wouldn't this be a problem?
    int fracDiv(int a, int b, int c, int d, int& num, int& den);
    defines fracDiv to take a, b, c, d, num, den

    fracDiv(numerator, denominator);
    calls fracDiv, but it only takes in 2 parameters instead of 6

    That said, I'm a newb

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    problem solved, i was just being stupid and i put the types in the function call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM