Go Down

Topic: Converting string to numeric (Read 11060 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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131