Re: strtof won't work

From: OzBob (i.dennison_at_btinternet.com)
Date: 08/13/04


Date: Fri, 13 Aug 2004 11:41:43 +0000 (UTC)

OK, I am having to re-type this manually because the VMware virtual machine
settings are stuffing me around WRT copy and paste.

Original scenario - this code is a sub-section of a 1200 line program, I am
trying to load values into an array with a 3 element structure (easy enough
to do in VB)

Snippets of code included as such,...

[TOP]
#include <stdlib.h> // Put first, just in case order of precedence is a
factor
#include <stdio.h> // Put second, but has also been compiled as first -
no change in results

struct ColumnArray
{
    char chCode;
    char chGroup;
    char *Value;
}

// Insert other procedures here

main()
{
    struct ColumnArray ColumnList[40];

    // Load up values
    ColumnList[1].Value = "15.88";
    // etc etc etc

    for(i=1;i<=5;i++)
    {
        printf("Element %i - text %s value %5.2f\n", i,
        ColumnList[i].Value, strtof(ColumnList[i].Value);
     }

}
[BOTTOM]

CONCLUSION: I wish to determine a decimal (dollar actually) value from a
text string, which will eventually be read from an array which is loaded
from a file - not exactly rocket science.
The above program gave such spurious results as "-1.5", "1.44", etc ,etc,
etc. I have used strtof function with "%5.2f" because they are both float -
I assume C supports some form of standards in this respect?

OK, it didn't work so I broke the problem down into the most likely
divisions and came up with the test program I first posted. Which gave the
same results as above (ie wrong ones).

Now I have created a program separate to this, here it is in its
entirety,...

[TOP]
#include <stdlib.h>
#include <stdio.h>

void main()
{
    char *test_string="15.99";

    printf("value = %5.2f\n", strtof(test_string, NULL));
}
[BOTTOM]

This produces output "0.00", which is different again to the original
problem. This just seems to compound the problems here.

Any input greatly appreciated, Ian

"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
news:VO86dwHAlJHBFw1W@robinton.demon.co.uk...
> In article <cfglv3$7n8$1@hercules.btinternet.com>, OzBob
> <i.dennison@btinternet.com> writes
> >RedHat Linux Ent 3, gcc 3.3.2
> >
> >// Start code
> >
> >char *test-string="15.88";
> >
> >printf ("String %s value %5.2f\n", test_string, strtof(&test_string));
> >
> >// End code
> >
> >produces the output "String 15.88 value -0.59".
> >
> >Uh, doesn't do what is says on the tin! (or the man page!) Anyone care to
> >explain?
>
> Why did you pass the address of the pointer to test-string rather than
> test-string which is a pointer to the string literal?
>
>
> --
> Francis Glassborow ACCU
> Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
> For project ideas and contributions:
http://www.spellen.org/youcandoit/projects