Solved

atof function returns 0/-1.#INF0000000

Posted on 1998-12-02
7
551 Views
Last Modified: 2009-07-29
Using the atof() function, I try convert both a CString and a char to a double. When I try to convert a number with 00 after the decimal point, ie "10"/"10.00" the function sometimes returns either 0 or -1.#INF00000, and sometimes the real values, ie 10.
What is my problem?
Thanks,
Alon
0
Comment
Question by:oak022097
7 Comments
 

Expert Comment

by:McCreary
ID: 1179104
I'd need to see a little of your code, but one thing you can not do is:

CString str = "10.0";
double x = atof(str);

What you can do is:

CString str = "10.0";
double x = atof( (const char*)str);
0
 
LVL 86

Accepted Solution

by:
jkr earned 60 total points
ID: 1179105
Your problem is 'atof()' - it is not _very_ reliable (and returns only float, and not double). I'd suggest 'strtod()' instead, which provides a more sophisticated way of error checking, e.g.

double d;
char* pc;
char* pszNum = "10.0";
CString strNum( "11.0");

d = strtod ( pszNum, &pc);

if (*pc)
{
 // conversion ended with sth. else than a null terminator, error
}


d = strtod ( ( LPCTSTR) strNum, &pc); // operator LPCTSTR() is overloaded!

if (*pc)
{
 // conversion ended with sth. else than a null terminator, error
}

d = strtod ( strNum->GetBuffer ( 0), &pc);

if (*pc)
{
 // conversion ended with sth. else than a null terminator, error
}






0
 

Author Comment

by:oak022097
ID: 1179106
I have tried to change the atof function into strtod, but got the same problem. I use the second example with the "(LPCTSTR)".
The variable I try to connvert is a DB field, it keeps a balance, and it has to be character type.
When I start the system, it works just fine, but then when I tried to update the field with the value "10", the strtod function returns 0. The *pc is null.

Thanks,
Alon
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 1179107
Try displaying the string you want to convert using 'AfxMessageBox()' - perhaps there's really "0" in it... If not, we'll have to take a closer look ;-)
0
 

Author Comment

by:oak022097
ID: 1179108
i put a breakpoint just before the strtod(), and saw that the value of the string is "10"
0
 
LVL 84

Expert Comment

by:ozo
ID: 1179109
//did you
#include <stdlib.h>
//or declare
double atof (const char *nptr);
0
 
LVL 86

Expert Comment

by:jkr
ID: 1179110
Could you post the code?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
Load and store *.pnm image file 1 75
how to convert c++ code to Android App 3 80
Exception thrown at 0x00007FFD5BC81F28 7 38
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now