problem with atof

Discussion in 'C Programming' started by Rudra Banerjee, Oct 28, 2012.

  1. 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.
     
    Rudra Banerjee, Oct 28, 2012
    #1
    1. Advertising

  2. Rudra Banerjee <> 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.
     
    Ben Bacarisse, Oct 28, 2012
    #2
    1. Advertising

  3. 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
     
    Rudra Banerjee, Oct 28, 2012
    #3
  4. Rudra Banerjee

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Oct 28, 2012
    #4
  5. Rudra Banerjee <> 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.
     
    Ben Bacarisse, Oct 28, 2012
    #5
  6. Rudra Banerjee

    BartC Guest

    "Rudra Banerjee" <> wrote in message
    news:...
    > 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
     
    BartC, Oct 28, 2012
    #6
  7. I am prepairing a minimal code that shows the problem.
    Please give me some time.
     
    Rudra Banerjee, Oct 28, 2012
    #7
  8. Rudra Banerjee

    James Kuyper Guest

    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
     
    James Kuyper, Oct 28, 2012
    #8
  9. On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:
    > Rudra Banerjee <> 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.
     
    Malcolm McLean, Oct 29, 2012
    #9
  10. Malcolm McLean <> writes:

    > On Sunday, October 28, 2012 12:15:54 PM UTC, Ben Bacarisse wrote:
    >> Rudra Banerjee <> 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.
     
    Ben Bacarisse, Oct 29, 2012
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Drew

    Why doesn't atof work?

    Drew, Jan 20, 2004, in forum: C++
    Replies:
    10
    Views:
    4,811
    Jeff Schwab
    Jan 23, 2004
  2. Pete C.

    atof() and _tstof() in VC6.0

    Pete C., Jun 24, 2004, in forum: C++
    Replies:
    9
    Views:
    7,271
    Ian Semmel
    Jun 24, 2004
  3. Replies:
    2
    Views:
    3,144
  4. Sharon
    Replies:
    4
    Views:
    1,023
    CBFalconer
    Nov 17, 2003
  5. Sreekanth

    atof() failure case

    Sreekanth, Apr 16, 2004, in forum: C Programming
    Replies:
    6
    Views:
    3,252
    CBFalconer
    Apr 16, 2004
Loading...

Share This Page