Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with atof

Reply
Thread Tools

problem with atof

 
 
Rudra Banerjee
Guest
Posts: n/a
 
      10-28-2012
const char *str_atype, *str_at1, *str_at2;
str_at1=gtk_entry_get_text(GTK_ENTRY(e->entry1));
str_at2=gtk_entry_get_text(GTK_ENTRY(e->entry2));
float i=atof(str_at1)-atof(str_at2);
print("%f",i);

This small piece of code snippet is giving me bit trouble. The problem is that if I use atoi, the results are coming fine (ofcourse as integer), but not if I use atof, as shown. For any value, i=0.00...means I assume atof did not worked at all.
why this is so?
gtk_entry_get_text is a gtk+ argument, which gets data as string.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-28-2012
Rudra Banerjee <(E-Mail Removed)> writes:

> const char *str_atype, *str_at1, *str_at2;
> str_at1=gtk_entry_get_text(GTK_ENTRY(e->entry1));
> str_at2=gtk_entry_get_text(GTK_ENTRY(e->entry2));
> float i=atof(str_at1)-atof(str_at2);
> print("%f",i);


Is that supposed to be printf? If so, it suggests that the posted code
is not the code that's being run. That's not a good idea as the problem
might be some detail that you've not transcribed. If it is 'print',
then I'd want to see what it does.

> This small piece of code snippet is giving me bit trouble. The problem
> is that if I use atoi, the results are coming fine (ofcourse as
> integer), but not if I use atof, as shown. For any value,
> i=0.00...means I assume atof did not worked at all. why this is so?
> gtk_entry_get_text is a gtk+ argument, which gets data as string.


First, what does printf("%s %s %f\n", str_at1, str_at2, i); produce?
Second, you might want to switch to using strtod since it can tell you
when things go wrong with the conversion.

--
Ben.
 
Reply With Quote
 
 
 
 
Rudra Banerjee
Guest
Posts: n/a
 
      10-28-2012
On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:
>>

> Is that supposed to be printf?

Not really, as this uses gtk library, this is g_print



> First, what does printf("%s %s %f\n", str_at1, str_at2, i); produce?
>
> Second, you might want to switch to using strtod since it can tell you
>


I have changed to
printf("%s %s %f\n",str_at1,str_at2,i);
which yeilds:
1 2.1 0.000000

if I change to strtod, I am getting seg. fault, with gdb yeilding:
Program received signal SIGSEGV, Segmentation fault.
0x0000003bca83d555 in ____strtod_l_internal () from /lib64/libc.so.6
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-28-2012
On 10/28/2012 7:42 AM, Rudra Banerjee wrote:
> const char *str_atype, *str_at1, *str_at2;
> str_at1=gtk_entry_get_text(GTK_ENTRY(e->entry1));
> str_at2=gtk_entry_get_text(GTK_ENTRY(e->entry2));
> float i=atof(str_at1)-atof(str_at2);
> print("%f",i);
>
> This small piece of code snippet is giving me bit trouble. The problem is that if I use atoi, the results are coming fine (ofcourse as integer), but not if I use atof, as shown. For any value, i=0.00...means I assume atof did not worked at all.
> why this is so?
> gtk_entry_get_text is a gtk+ argument, which gets data as string.


Did you #include <stdlib.h>?

(If that's not the problem, please post a *short* and
*complete* and *self-contained* program that demonstrates
the difficulty. As things stand, I can only guess what
the GTK stuff does, and what strings might be involved,
and the behavior of print() is a complete mystery.)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-28-2012
Rudra Banerjee <(E-Mail Removed)> writes:

> On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:
>>>

>> Is that supposed to be printf?

> Not really, as this uses gtk library, this is g_print


OK, but the main point remains: I have not see the code that is actually
running. That makes debugging impossible. All I can do it make vague
guesses.

>> First, what does printf("%s %s %f\n", str_at1, str_at2, i); produce?
>>
>> Second, you might want to switch to using strtod since it can tell you

>
> I have changed to
> printf("%s %s %f\n",str_at1,str_at2,i);
> which yeilds:
> 1 2.1 0.000000


That means the problem is not in the code you posted. I'd put a small
bet on it being what Eric as suggested -- a missing header file (well, a
missing declaration caused by a missing header file) -- but it's just a
guess.

You'll probably find the error by yourself just by trying to make a
minimal example that shows the problem. That's the main benefit of
giving that advice.

> if I change to strtod, I am getting seg. fault, with gdb yeilding:
> Program received signal SIGSEGV, Segmentation fault.
> 0x0000003bca83d555 in ____strtod_l_internal () from /lib64/libc.so.6


What is anyone expected to do with that? You don't even show the call
to strtod.

--
Ben.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      10-28-2012


"Rudra Banerjee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:
>>>

>> Is that supposed to be printf?

> Not really, as this uses gtk library, this is g_print
>
>
>
>> First, what does printf("%s %s %f\n", str_at1, str_at2, i); produce?
>>
>> Second, you might want to switch to using strtod since it can tell you
>>

>
> I have changed to
> printf("%s %s %f\n",str_at1,str_at2,i);
> which yeilds:
> 1 2.1 0.000000


Do str_at1 and str_at2 actually contain exactly "1" and "2.1", both
zero-terminated without any invisible control characters? (Perhaps show
strlen() of each, which should be 1 and 3.)

Also, you are substracting one atof() result from another. Since the result
is in dispute, try showing each atof() result separately.

--
Bartc

 
Reply With Quote
 
Rudra Banerjee
Guest
Posts: n/a
 
      10-28-2012
I am prepairing a minimal code that shows the problem.
Please give me some time.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-28-2012
On 10/28/2012 08:44 AM, Rudra Banerjee wrote:
> On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:

....
>> Second, you might want to switch to using strtod since it can tell you

....
> if I change to strtod, I am getting seg. fault, with gdb yeilding:
> Program received signal SIGSEGV, Segmentation fault.
> 0x0000003bca83d555 in ____strtod_l_internal () from /lib64/libc.so.6


strtod() is better than atof() because, when used correctly, it always
has defined behavior, which includes giving you useful information about
what went wrong if there was a problem with the string to be converted.
However, using strtod() correctly is more complicated than using atof().
The above message implies that you used it incorrectly - however,
without seeing your code, we can't be sure what was incorrect about the
way you used it. Specifically, a segmentation fault implies some kind of
problem with pointers, but a call to strtod(nptr, endptr) gives strtod()
three different pointers that could cause problems: nptr, endptr, and
*endptr.
--
James Kuyper
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      10-29-2012
On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:
> Rudra Banerjee <(E-Mail Removed)> writes:
>
>
>
> First, what does printf("%s %s %f\n", str_at1, str_at2, i); produce?
>

For printf() diagnostics, printf("***%s***\n", str_at1);

Often the problem is wrong embedded spaces, and by putting the asterisks
round, it's easier to spot them.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-29-2012
Malcolm McLean <(E-Mail Removed)> writes:

> On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:
>> Rudra Banerjee <(E-Mail Removed)> writes:
>>
>>
>>
>> First, what does printf("%s %s %f\n", str_at1, str_at2, i); produce?
>>

> For printf() diagnostics, printf("***%s***\n", str_at1);
>
> Often the problem is wrong embedded spaces, and by putting the asterisks
> round, it's easier to spot them.


Good point. I use "[%s]" but the purpose is the same.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
locale independent atof? sprotty@hotmail.com C++ 2 01-17-2005 03:37 PM
atof() and _tstof() in VC6.0 Pete C. C++ 9 06-24-2004 09:38 PM
atof() failure case Sreekanth C Programming 6 04-16-2004 11:11 AM
Why doesn't atof work? Drew C++ 10 01-23-2004 01:27 AM
what are the reverse functions of atof( ), atoi( ) and atol( ) ? Sharon C Programming 4 11-17-2003 10:13 AM



Advertisments