Thread: Strange function problem!

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30

    Strange function problem!

    Hi, whilst trying to pass 2 floats into a function outside of main the values printed are different from what they are in main. Integers also passed into the function remain fine....

    Code:
    int main ( void ){
    //blablabla
    
    printf("\n%.2f %.2f\n ",xcent,ycent); //print fine here
    float denom = denominator(n,xValues,yValues,xcent,ycent);
    }
    
    float denominator(int n, int *x, int *y, float xc, float yc){
    printf("%i %i %i %i %f %f", n, x[0], y[0], xc, yc);//ints print fine, floats print messed up
    //blablabla
    }
    wtf?

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Soooo..... How are they being printed differently?
    You use some extra format specifiers in main(), but not in denominator().

    A better description (say, example?) of your problem with help with getting you help.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile with warnings. I am ever so unsure about implicit int, but this sounds like a case of it: you probably should forward declare the function denominator.
    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 hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    printf("%i %i %i %i %f %f", n, x[0], y[0], xc, yc);
    You have 6 format specifiers in that printf statement but you only provide 5 variables.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    I declared denominator at the top but there was no change, when compiled with warnings i get one saying:
    27 : warning: int format, double arg (arg 5)
    27 : warning: too few arguments for format
    line 27 is the second printf mentioned above!

  6. #6
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Hahahaha massive fail, good spot though!
    thats got everything working great!

    thankyouuuu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM