Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# atof function returns 0/-1.#INF0000000

Posted on 1998-12-02
Medium Priority
609 Views
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
Question by:oak022097
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Expert Comment

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

jkr earned 120 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

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

LVL 86

Expert Comment

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

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

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

LVL 86

Expert Comment

ID: 1179110
Could you post the code?
0

## Featured Post

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [sâ€¦
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why tâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
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.
###### Suggested Courses
Course of the Month11 days, 23 hours left to enroll