473,804 Members | 4,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4495
"Sreekanth" <sr*******@yaho o.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*******@free net.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*******@free net.de> wrote in message
news:72******** *************** *********@4ax.c om...
"Sreekanth" <sr*******@yaho o.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*******@free net.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*******@yaho o.com> wrote:
"Irrwahn Grausewitz" <ir*******@free net.de> wrote in message
news:72******** *************** *********@4ax.c om...
"Sreekanth" <sr*******@yaho o.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***********@p hysik.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*******@yaho o.com> wrote:
"Irrwahn Grausewitz" <ir*******@free net.de> wrote in message
news:72******** *************** *********@4ax.c om...
"Sreekanth" <sr*******@yaho o.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***********@p hysik.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**********@n ews.mch.sbs.de> "Sreekanth" <sr*******@yaho o.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*******@free net.de> wrote in message
news:72******* *************** **********@4ax. com...
"Sreekanth" <sr*******@yaho o.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
4811
by: Drew | last post by:
Hi: I'm having trouble getting atof() to accurately convert "3.1" Any ideas? Thank you,
6
397
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 suggest me the way to differentiate an invalid float number string and a 0.0. TIA, Sreekanth.
15
13086
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 = 97.000000
20
2189
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 this? Trond Valen
21
8369
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 the exact value that is passed.
5
3203
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 afraid it might contain bugs though. If I serialize a double, I get a string of the format "-1.0000000000000000e+212". This string gets stored in the buffer. Then, to convert it back into a double, I pass it to the atof function. The problem...
14
8920
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) { istringstream(s); T val; is >val;
5
6557
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 since I am so very keen to learn to code in standard C. Could someone tell me the long double equivalent of atof()? I was getting some peculiar behaviour in a little bit of math code I am working with. I thought the problem was in the math...
2
8899
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.