Go Down

Topic: Converting string to numeric (Read 6604 times) previous topic - next topic


Jan 17, 2011, 04:24 am Last Edit: Jan 17, 2011, 04:26 am by jgubits Reason: 1
In my program I need to convert a String variable to numeric float variable.

For example:
String Weight="1234.56";
float WVal=atof(Weight);

I'm getting error:  cannot convert 'String' to 'const char*' for argument '1' to 'double atof(const char*)'

Why is that error? is there a different way to convert a sting to numeric?
I would appreciate any help



Jan 17, 2011, 06:16 am Last Edit: Jan 17, 2011, 06:53 am by mrtaylor Reason: 1
The simplified answer:
a) the String library is a class (object).   A class can contain member variables, constants, member functions, and overloaded operators. Its a c++ thing
b) atof expect an argument of type const char * str
c) to convert the string class to a const char * str

Code: [Select]
char buf[Weight.length()];
float WVal=atof(buf);


Just as a side note, I dislike converting to different types. It can inject errors and drive you crazy debugging.  I find it better in the long run to redesign the code to use the type I need, if possible.

Go Up