problem with atof

R

Rudra Banerjee

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.
 
B

Ben Bacarisse

Rudra Banerjee said:
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.
 
R

Rudra Banerjee

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
 
E

Eric Sosman

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.)
 
B

Ben Bacarisse

Rudra Banerjee said:
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.
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.
 
B

BartC

Rudra Banerjee said:
Not really, as this uses gtk library, this is g_print




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.
 
R

Rudra Banerjee

I am prepairing a minimal code that shows the problem.
Please give me some time.
 
J

James Kuyper

....
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.
 
M

Malcolm McLean

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.
 
B

Ben Bacarisse

Malcolm McLean said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top