Thread: Char array to a float?

  1. #1
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34

    Char array to a float?

    Is there a function in any of the standard libraries that does this, or do I have ot create my own?

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    You mean take a null terminated array of characters that contains the textual representation of a floating point number and return said floating point number?

    The function is atof() //(ascii to float)

    It takes a pointer to the array of char and returns a double precision floating pooint value.

    It is found in <cstdlib>.

  3. #3
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Yep, that's what I needed, thanks.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    strtod() would be better choice.

  5. #5
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    So... what's the second paramiter of strtod() supposed to be?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Nippashish
    So... what's the second paramiter of strtod() supposed to be?
    Did you look it up?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What am I saying! This is a C++ board, use a stringstream.
    Although if you still want info on strtod() go here: http://www.opengroup.org/onlinepubs/...sh/strtod.html

    An example of a stringstream converting a char * to a float:
    Code:
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int main () 
    {
    	float val;
    	stringstream ss (stringstream::in | stringstream::out);
    	
    	ss << "1.2";
    	
    	ss >> val;
    	cout << val*2 << endl;
    	
    	return 0;
    }
    What part of Nova Scotia do ye hail from?
    Last edited by Eibro; 11-03-2002 at 08:34 PM.

  8. #8
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Was thinking the same thing, Elbro.

    Why are you using a nul terminated array of char anyway, nippa? Strings are almost always better.

  9. #9
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    I never really used strings before so it just never occured to me, I'm using them now though, and it's making my life a hell of a lot easier .

    And I'm from the Bridgewater area.
    Last edited by Nippashish; 11-03-2002 at 10:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  4. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM