[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

atof function returns 0/-1.#INF0000000

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
oak022097
Asked:
oak022097
1 Solution
 
McCrearyCommented:
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
 
jkrCommented:
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
 
oak022097Author Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
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
 
oak022097Author Commented:
i put a breakpoint just before the strtod(), and saw that the value of the string is "10"
0
 
ozoCommented:
//did you
#include <stdlib.h>
//or declare
double atof (const char *nptr);
0
 
jkrCommented:
Could you post the code?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now