Solved

atof function returns 0/-1.#INF0000000

Posted on 1998-12-02
7
539 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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

20 Experts available now in Live!

Get 1:1 Help Now