Thread: Problems with module

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    Problems with module

    Hey everyone,
    I'm sure at the end it's just a stupid mistake, but I need some help at a something i'm writing on atm.
    The idea is to add, subtract etc. fractions. I want to put the functions in a header and call them in the programm itself.
    My problem is, that it doesnt recognize the functions i wrote in the .c of the module.

    I'm a total beginner so pls no hate

    The .h
    Code:
    #ifndef rational_h#define rational_h
    #endif
    
    
    typedef struct {
        long numerator, denumerator;
    }rational;
    
    
    rational fraction1;
    rational fraction2;
    rational result;
    The .c for the module
    Code:
    #include "rational.h"
    
    
    
    
    
    
    rational addition(rational fraction1, rational fraction2,rational result)
    {
        result.numerator = fraction1.numerator + fraction2.numerator;
        result.denumerator = fraction1.denumerator;
        return result;
    };
    
    
    
    
    
    
    rational subtraction(rational fraction1, rational fraction2, rational result)
    {
        result.numerator = fraction1.numerator-fraction2.numerator;
        result.denumerator = fraction1.denumerator;
        return result;
    };
    
    
    
    
    
    
    rational multiplication(rational fraction1, rational fraction2, rational result)
    {
        result.numerator = fraction1.numerator * fraction2.numerator;
        result.denumerator = fraction1.denumerator;
        return result;
    };
    
    
    
    
    
    
    rational division(rational fraction1, rational fraction2, rational result)
    {
        long LMC;
    
    
        result.numerator = fraction1.numerator * fraction2.denumerator;
        result.denumerator = fraction1.denumerator*fraction2.numerator;
    
    
        if (fraction1.denumerator < fraction2.denumerator)
        {
            LMC=ggT(fraction1.denumerator, fraction2.denumerator);
        }
        else
        {
            LMC = ggT(fraction2.denumerator, fraction1.denumerator);
        }
    
    
        result.numerator *= (LMC / result.denumerator);
        result.denumerator = LMC;
        
        return result;
    };
    
    
    
    
    
    
    long ggT(long a, long b) 
    {
        static long helper = 0;
        helper += b;
    
    
    
    
    
    
        if (helper%a == 0 && helper%b == 0)
        {
            return helper;
        }
        else 
        {
            return ggT(a, b);
        }
        
       
    
    
    }
    The program itself (in between the functions im gonna add something for the output)
    Code:
    #include "rational.h"
    
    int main(void)
    {
        addition(fraction1, fraction2);
        subtraction(fraction1, fraction2);
        division(fraction1, fraction2);
        multiplication(fraction1, fraction2);
    
    
        return 0;
    }
    Thanks everyone

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need to declare the functions in the header file, like so:

    Code:
    #ifndef RATIONAL_H
    #define RATIONAL_H
    
    typedef struct {
        long numerator, denumerator;
    }rational;
    
    rational addition(rational fraction1, rational fraction2,rational result);
    rational subtraction(rational fraction1, rational fraction2, rational result);
    rational multiplication(rational fraction1, rational fraction2, rational result);
    rational division(rational fraction1, rational fraction2, rational result);
    long ggT(long a, long b);
    
    #endif
    Also note the #endif goes at the end, since the whole header should be wrapped in the #define.

    You should not declare variables in a header file, and should avoid global variables in general. Those variables can be declared locally where needed.

    Your function calls don't provide enough arguments to the function:

    Code:
    // function call with two arguments
    addition(fraction1, fraction2);
    
    // function has three parameters
    rational addition(rational fraction1, rational fraction2,rational result)
    {
        result.numerator = fraction1.numerator + fraction2.numerator;
        result.denumerator = fraction1.denumerator;
        return result;
    };
    If you're returning "result", it does not have to be an argument.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your header should look like this:
    Code:
    #ifndef RATIONAL_H
    #define RATIONAL_H
    
    typedef struct {
        long numerator, denumerator;
    } rational;
    
    rational addition(rational fraction1, rational fraction2,rational result);
    
    #endif
    Notice that the header inclusion guard now encloses the content of the header. I have removed the global variable declarations, partly because you don't need them, and partly because if you did need them, typically they would be declared extern in the header:
    Code:
    extern rational fraction1;
    extern rational fraction2;
    extern rational result;
    then declared (and hence defined) in exactly one source file.

    Finally, I have added one related function declaration. You should add the remaining ones.

    Of course, since the header no longer has the global variable declarations, you should declare those variables as local to the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    rational.h:
    Move #endif to end
    Add function prototypes
    Remove definitions of fraction1, fraction2, result
    "denumerator" (not a word) should be "denominator".

    rational.c:
    Get rid of semicolons after final brace of functions.
    "addition" and "subtraction" need to convert both fractions to the same denominator (presumably through cross-multiplication).
    "multiplication" needs to multiply the denominators.
    I don't know what ggT is supposed to be. Usually you would use a gcd (greatest common divisor) function to help reduce the fractions to lowest terms. It would need to be used for all the operations.

    main.c:
    define fraction1, fraction2, result.
    Accept return value in result.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    First off, thanks to everyone

    @john.c
    rational.h:
    -what do you mean by prototypes?
    -english skills on point

    rational.c:
    -I did not convert the denominators, because of ggT. It's supposed to be a recursive approach on finding the lowest common divisor. I want to add this function in the part where the user inputs the fractions. So that I can change them there
    This way I also only need to muliply the nominators
    -and what do you mean with your last sentence?

    Thank you a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Module Help!
    By J-Camz in forum C Programming
    Replies: 10
    Last Post: 11-27-2008, 08:29 PM
  2. Speed using only a module
    By Kempelen in forum C Programming
    Replies: 8
    Last Post: 07-15-2008, 06:27 AM
  3. module without using %
    By KIBO in forum C++ Programming
    Replies: 9
    Last Post: 01-16-2008, 11:24 PM
  4. module.h
    By podiyan in forum Linux Programming
    Replies: 3
    Last Post: 10-24-2007, 05:53 AM
  5. Merge Module Problems
    By mercury529 in forum Windows Programming
    Replies: 0
    Last Post: 11-29-2006, 03:30 PM

Tags for this Thread