473,324 Members | 2,239 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,324 software developers and data experts.

atof() failure case

Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate an
invalid float number string and a 0.0.
TIA,
Sreekanth.
Nov 14 '05 #1
6 4452
"Sreekanth" <sr*******@yahoo.com> wrote:
Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate an
invalid float number string and a 0.0.


Instead of atof use strtod.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #2
Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.

"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com...
"Sreekanth" <sr*******@yahoo.com> wrote:
Hello,
Am trying to convert a string to float. Am using atof() for that purpose.
But the return value for atof is same for the string "0.0" and for some
invalid input "Invalid". Can any body suggest me the way to differentiate aninvalid float number string and a 0.0.


Instead of atof use strtod.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html

Nov 14 '05 #3
Sreekanth <sr*******@yahoo.com> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com...
"Sreekanth" <sr*******@yahoo.com> wrote:
>Hello,
>Am trying to convert a string to float. Am using atof() for that purpose.
>But the return value for atof is same for the string "0.0" and for some
>invalid input "Invalid". Can any body suggest me the way to differentiate an >invalid float number string and a 0.0.
Instead of atof use strtod.

Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.


Well, that's why you should use strtod():

double strtod(const char *nptr, char **endptr);

If you got an illegal argument *endptr and nptr are identical after
the call of the function. On legal input what endptr points to is
a pointer to the part of the input string where the conversion
stopped and it differs from nptr.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
Thanks Gentleman.. Working..

<Je***********@physik.fu-berlin.de> wrote in message
news:c5************@uni-berlin.de...
Sreekanth <sr*******@yahoo.com> wrote:
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com...
"Sreekanth" <sr*******@yahoo.com> wrote:
>Hello,
>Am trying to convert a string to float. Am using atof() for that purpose. >But the return value for atof is same for the string "0.0" and for some >invalid input "Invalid". Can any body suggest me the way to
differentiate an
>invalid float number string and a 0.0.

Instead of atof use strtod.
Thanks, But it also returns 0 in both cases.. I want to differentiate an
invalid string and a Zero string in a string to float conversion

mechanism.
Well, that's why you should use strtod():

double strtod(const char *nptr, char **endptr);

If you got an illegal argument *endptr and nptr are identical after
the call of the function. On legal input what endptr points to is
a pointer to the part of the input string where the conversion
stopped and it differs from nptr.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de

Nov 14 '05 #5
Sreekanth wrote:

Am trying to convert a string to float. Am using atof() for that
purpose. But the return value for atof is same for the string "0.0"
and for some invalid input "Invalid". Can any body suggest me the
way to differentiate an invalid float number string and a 0.0.


strtod();

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #6
In <c5**********@news.mch.sbs.de> "Sreekanth" <sr*******@yahoo.com> writes:
Thanks, But it also returns 0 in both cases..
It also provides enough clues to make the disambiguation trivial.
I want to differentiate an
invalid string and a Zero string in a string to float conversion mechanism.
Either use the clues provided by strtod, or use atof and do some minimal
parsing of the string yourself, when the result of the conversion is 0.

Dan
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:72********************************@4ax.com.. .
"Sreekanth" <sr*******@yahoo.com> wrote:
>Hello,
>Am trying to convert a string to float. Am using atof() for that purpose.
>But the return value for atof is same for the string "0.0" and for some
>invalid input "Invalid". Can any body suggest me the way to differentiatean >invalid float number string and a 0.0.


Instead of atof use strtod.


--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

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

Similar topics

10
by: Drew | last post by:
Hi: I'm having trouble getting atof() to accurately convert "3.1" Any ideas? Thank you,
6
by: Sreekanth | last post by:
Hello, Am trying to convert a string to float. Am using atof() for that purpose. But the return value for atof is same for the string "0.0" and for some invalid input "Invalid". Can any body...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
20
by: Trond Valen | last post by:
Hi! Stupid atof, it returns 0.0 when it tries to parse something like "fish". So I don't know whether the number was really 0 or a string that couldn't be parsed. Is there a better way to do...
21
by: oksuresh | last post by:
Hi talents, I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 But I would like to have...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
14
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) {...
5
by: lcw1964 | last post by:
Greetings again, I will burden the group with yet another tenderfoot question, but since conscientious googling hasn't yield a lucid answer I thought I would risk the shortcut of asking here...
2
by: allexander | last post by:
Hello ! I need to convert a string like string number = "1235646.5678" to any floating point number x (double x or float x) I have used earlier the function atof()
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.