By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

returning a float

P: n/a
I am trying to write a function that returns a float. This is what the
function should do: It takes the socket number and a variable name.
The function looks up the variable in an array of structs stored on a
server. The string (of a float number) stored is returned. The
function works except that it does not return a float. It retreives
the string fine, but I don't know why it is not converting the string
to a float. What am I doing wrong?

Thanks,
Mike

float readFloatVar(const int sock, const char *varname)

{

int n, size; /* socket descriptor, read count */

char *rpack, temp[64];
float returnvar;
readdelpacket.requesttype=2;

strcpy(readdelpacket.varname,varname);

write(sock, &readdelpacket, sizeof(readdelpacket)); /*request varible
name */

rpack=(char*)(&rpacket);
size=sizeof(rpacket);
n=0;

while ((n=read(sock, (void*)rpack, size)) >0){
size-=n;
rpack+=n;
} /* read response from server */
if (n < 0)

errexit("client socket read failed: %s\n", strerror(errno));

if (rpacket.rescode < 0)
returnvar = rpacket.rescode; /*variable not found */
else {
strcpy(temp,rpacket.rvarval); /*rpacket.rvarval is a char[64] */

printf("stubs rpacket.rvarval %s \n", temp); /* shows proper value
22333.3324 */
printf("ftubs rpacket.rvarval %15.5f \n", atof(temp)); /* shows
0.00000 should show 22333.3324*/
returnvar = atof(temp);
}
return returnvar;

}

Apr 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a


bo******@hotmail.com wrote On 04/24/06 11:21,:
I am trying to write a function that returns a float. This is what the
function should do: It takes the socket number and a variable name.
The function looks up the variable in an array of structs stored on a
server. The string (of a float number) stored is returned. The
function works except that it does not return a float. It retreives
the string fine, but I don't know why it is not converting the string
to a float. What am I doing wrong?


Impossible to say with certainty, since the code
you provided is incomplete. At a guess, though, you
forgot to #include <stdlib.h>.

Two words of advice. First, when you have some
code whose behavior baffles you, post all of the code
and not just a few snippets or paraphrases. After all,
the very fact that you don't understand why the code
behaves as it does means that you are unqualified to
decide what parts are relevant and what are unimportant.
In the instance at hand you have shortened your posting
by omitting some "obvious" context, but it is likely
that the context itself is what is wrong.

Second, when posting complete code you should first
make an effort to strip away all distractions and side-
issues. Your question was about why the function didn't
seem to be converting a string to a float value, and the
fact that the string came from a socket connection had
nothing to do with the matter. You could have whittled
it all away and posted the following complete program
instead:

#include <stdio.h>
float readFloatVar(void);
int main(void) {
printf ("returned: %f\n", readFloatVar());
return 0;
}
float readFloatVar(void) {
char *string = "22333.3324";
return atof(string);
}

.... and the omission of <stdlib.h> would have been obvious
to all. Indeed, in the process of whittling down you might
well have discovered the problem for yourself.

Now, these two pieces of advice may seem diametrically
opposed: I say that you lack the understanding to whittle
effectively, and then encourage you to do it. That's where
the "complete program" part comes in: Whittle a bit, run the
program, and see what it does. If it still misbehaves, whittle
some more and try again; you'll eventually arrive at a short
program that demonstrates your essential problem unencumbered
by off-topic distractions. If at some point the program stops
misbehaving, you'll know that the problem had something to do
with the material most recently removed, and this clue may well
enable you to solve the mystery for yourself -- even if it
doesn't, adding the valuable clue to your question may help
someone else solve it for you.

If you're going to be a programmer, you need to practice
the skill of asking good questions. The process of debugging
is a process of forming and answering questions; if you learn
to ask good questions you'll be much more successful.

Oh, and another hint for success: Learn how to crank up
your compiler's warning levels. Many compilers, if properly
asked, would have pointed out your probably error quite
clearly.

--
Er*********@sun.com

Apr 24 '06 #2

P: n/a
bo******@hotmail.com wrote:
.... The
function works except that it does not return a float. It retreives
the string fine, but I don't know why it is not converting the string
to a float. What am I doing wrong? <...>
strcpy(temp,rpacket.rvarval); /*rpacket.rvarval is a char[64] */

printf("stubs rpacket.rvarval %s \n", temp); /* shows proper value
22333.3324 */
printf("ftubs rpacket.rvarval %15.5f \n", atof(temp)); /* shows
0.00000 should show 22333.3324*/
returnvar = atof(temp);
}
return returnvar;


Have you included the header file where atof() is declared? It may be
implicitly declared as "int atof()" Try compiling with the maximum
warning level for your compiler.

Apr 24 '06 #3

P: n/a
In article <11*********************@g10g2000cwb.googlegroups. com>,
<bo******@hotmail.com> wrote:
I am trying to write a function that returns a float. float readFloatVar(const int sock, const char *varname)
{ char *rpack, temp[64];
float returnvar; printf("stubs rpacket.rvarval %s \n", temp); /* shows proper value
22333.3324 */
printf("ftubs rpacket.rvarval %15.5f \n", atof(temp)); /* shows
0.00000 should show 22333.3324*/
returnvar = atof(temp);
}
return returnvar;
}


The other poster's point about needing <stdlib.h> for the atof()
prototype sounds quite plausible as the cause of the blantant symptoms
you see. Allow me, though, to point out a more subtle issue that
might or might not be of concern to you.

atof() is prototyped to return a double, not a float. In the printf()
with the %15.5f format, this is not an issue as the %f format is
defined to expect a double (not a float.)

On the line below that where you assign the result of atof() into
returnvar, you are assigning a double into a float. float almost always
has less precision and less range than double, so depending on what the
string values retrieved from the server contain, you could
potentially run into the situation where a retrieved value prints out
perfectly from the printf() but looks noticably different as a float
(due to roundoff), or in which the value does not fit into a float
[the result of which is undefined.]

Many compilers will give a warning about storing a double into
a float, but they are not required to do so at all, or might only do so
if you have specifically asked for extensive warnings [the
mechanism for so requesting would be compiler dependant.]
--
Programming is what happens while you're busy making other plans.
Apr 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.