473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

returning a float

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
3 2660


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
19
by: Steven T. Hatton | last post by:
I believe it is technically possible to return a pointer to the first element of an array. I can persuade the returned pointer to act like an array, with some qualifications. I'm specifically...
5
by: beto | last post by:
A am receiving an error when attempting to return a float from a C function. The following is the C code #define DLL __declspec(dllexport) DLL float testfloat() { return 3.3; }
10
by: randomtalk | last post by:
hello, i have another problem i feel that i have to be missing something.. Basically, i've written a recursive function to find all the prime up to a number (lim).. here is the function: The...
11
by: jza | last post by:
Hello all, I am fairly new to c, coming from a Java background. I am working on a mathematical program and I have a function that needs to return a 2-d array. After some web searching, I have...
5
by: prads | last post by:
Hello, the following code doesnot return the corect values to the main function. However it prints the correct values within the function. Also the same when written in C works absolutely fine. Pls...
1
by: soospecial81 | last post by:
This lab will allow you to practice using value returning functions Please read all directions before beginning. You will turn in two programs (L5_P1.cpp and L5_P2.cpp). Be sure to include you and...
11
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.