473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem converting a string to a float value

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

Jul 9 '06 #1
3 2144
de*********@gmail.com wrote:
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.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Jul 9 '06 #2
>>#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

Jul 9 '06 #3
de*********@gmail.com writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
1
by: Meya-awe | last post by:
Hello there, I am using a 3rd party library and a function which requires a float value in one of its methods. My data is in a string format and i tried using Convert.ToDouble and...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
5
by: vivekaseeja | last post by:
Hi , Trying to convert a string value to a float value after reading the value from an XML file , but not sure what function to use. The following only works for integers Int32.Parse...
8
by: SpreadTooThin | last post by:
Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', ) b = array.array('H', ) b = a
10
by: Alistair King | last post by:
Jon Clements wrote: sorry, this has been a little rushed XDS is defined before the function and is a float. the Fxas values are also and they are integers
18
by: atv | last post by:
at least to me it is. I can't figure out for the life what it is i'm doing wrong here. i have a function called assign_coordinate. usually, i pass a malloced pointer to it, then individual...
1
Brutus
by: Brutus | last post by:
I'm a total newbie in C# and ASP.NET, so I hope I'm in the right forum. My very first project is a custom control that converts strings into images on the fly (through a httphandler) - that works...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.