Is there a function in any of the standard libraries that does this, or do I have ot create my own?
Printable View
Is there a function in any of the standard libraries that does this, or do I have ot create my own?
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>.
Yep, that's what I needed, thanks.
strtod() would be better choice.
So... what's the second paramiter of strtod() supposed to be?
Did you look it up?Quote:
Originally posted by Nippashish
So... what's the second paramiter of strtod() supposed to be?
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:
What part of Nova Scotia do ye hail from?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;
}
Was thinking the same thing, Elbro.
Why are you using a nul terminated array of char anyway, nippa? Strings are almost always better.
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 :D.
And I'm from the Bridgewater area.