Problem converting a string to a float value

D

dean.elwood

Hi guys,

My first post here and I'm a pascal coder doing his best to move to
C/C++ so please go easy on me ;)

I'm using the MySQL C API to pull some values from a DB. One of the
fields is a float value, but I can't seem to cast this field to a float
variable correctly:-

if(mysql_exec_sql(&mysql,query)==0) /*success*/
{
printf( "%ld Record Found\n",(long) mysql_affected_rows(&mysql));
result = mysql_store_result(&mysql);
if (result) // there are rows
{
num_fields = mysql_num_fields(result);

while ((row = mysql_fetch_row(result)))
{
float total;
total = atof(row[7]);

printf("%-10s", row[7]);
printf("%f", total);

printf("\n");
}

mysql_free_result(result);
}
}

The first one shows the correct value when I show it as a string, the
second one always seems to be "NAN".

I've done some googling around but can't seem to find the answer. I
basically want to grab this field as a float value so that I can do
some maths on it. I beleive this field is, in current form, a string.

Any help would be much appreciated.

Thanks,

Dean
 
E

Eric Sosman

Hi guys,

My first post here and I'm a pascal coder doing his best to move to
C/C++ so please go easy on me ;)

I'm using the MySQL C API to pull some values from a DB. One of the
fields is a float value, but I can't seem to cast this field to a float
variable correctly:-

if(mysql_exec_sql(&mysql,query)==0) /*success*/
{
printf( "%ld Record Found\n",(long) mysql_affected_rows(&mysql));
result = mysql_store_result(&mysql);
if (result) // there are rows
{
num_fields = mysql_num_fields(result);

while ((row = mysql_fetch_row(result)))
{
float total;
total = atof(row[7]);

printf("%-10s", row[7]);
printf("%f", total);

printf("\n");
}

mysql_free_result(result);
}
}

The first one shows the correct value when I show it as a string, the
second one always seems to be "NAN".

I've done some googling around but can't seem to find the answer. I
basically want to grab this field as a float value so that I can do
some maths on it. I beleive this field is, in current form, a string.

Any help would be much appreciated.

Just a guess: Did you forget to #include <stdlib.h>?
(By the way, strtod() is usually a better choice than atof(),
since it gives you a chance to detect, e.g., "123B" as a
string that ought to be numeric but isn't.)
 
D

dean.elwood

#include <stdlib.h><<

Oh dear, how stupid do I feel? ;)

Thanks Eric - that was it.

Thanks for the tip about atof not being the best also - will try that.

Dean
 
K

Keith Thompson

Hi guys,

My first post here and I'm a pascal coder doing his best to move to
C/C++ so please go easy on me ;)

Welcome.

One of the first things you need to learn is that there's no such
thing as "C/C++" (or, more precisely, there's no agreement on what
"C/C++" might mean).

C and C++ are two different languages. They're very closely related
(C++ is almost, but not quite, a superset of C), but still quite
distinct.

If you wanted to talk about C++, the place to do so would be
comp.lang.c++. If you want to talk about C, you're in the right
place. (There was no C++ in your orginal post, other than the mention
of "C/C++", so this is meant to be informative, not a complaint.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top