Thread: Need some help with atof()

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    Need some help with atof()

    Code:
    aperture is a string
    int ApertureNumber;
    ApertureNumber = atof(&aperture[1]);
    Will this work if aperture is a string ? If not how would I do this ,
    convert string --> float ( it needs to start reading from the second character ).
    I had a look at Taurus's thread , and iI want to do exactly the same as what him , however unlike him I'm using strings - should I use strtol() instead ? If that is the case can you explain how would I use it in this case ? Thanks a lot

    http://cboard.cprogramming.com/showthread.php?t=93602

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Will this work if aperture is a string ?
    Yes, &aperture[1] is correct. Or you can also use aperture+1 (no ampersand).

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    When I compile it , I get 0.0000000 for the value :
    Code:
    						int ApertureNumber;
    						ApertureNumber = atof(&guess[1]);
    						fprintf(output,"The integer representation is : %f",&ApertureNumber);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > fprintf(output,"The integer representation is : %f",&ApertureNumber);
    Oh boy, you really need to pay attention more.
    You keep adding and removing things at random, like where the hell did this & come from for printing the result?

    Oh, and why is this yet another thread on the same small problem?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fprintf(output,"The integer representation is : %f",&ApertureNumber);
    And %f is for floats, use %d for ints.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by swoopy View Post
    > fprintf(output,"The integer representation is : %f",&ApertureNumber);
    And %f is for floats, use %d for ints.
    atof returns double, so logically ApertureNumber should be made double and printed using %f format
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    > You keep adding and removing things at random, like where the hell did this & come from for printing the result?
    I'm not sure I understand , are you saying it should be :
    Code:
    fprintf(output,"The integer representation is : %f", ApertureNumber);
    without an & ?

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    fprintf(output,"The integer representation is : %f", ApertureNumber);
    Have a look at this example fprintf

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by vart View Post
    atof returns double, so logically ApertureNumber should be made double and printed using %f format
    Then the comment in the fprintf() is bogus.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    ............
    Last edited by ICool; 09-23-2007 at 09:41 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Because you're passing an int to printf, and it's expecting a float.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    So float ApertureNumber ? I tried it just now and it retrns a huge number

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <stdlib.h>
    would help a lot.

    Kurt

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    IT WORKS Thank you VERY much for that ZuK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atof function question
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-13-2008, 09:43 PM
  2. problem with atof
    By ssharish in forum C Programming
    Replies: 17
    Last Post: 03-24-2005, 07:53 AM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. atof()
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2003, 10:08 AM
  5. how do atof, atoi, and atol work?
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 09:25 PM