# Thread: A basic math programming question

1. The BEST version to convert a string to float is strtof() - it returns a float, but also tells you if it went OK or not.

http://www.hmug.org/man/3/strtod.php

--
Mats

2. Prelude,
I see your point. I do want to avoid such inputs. In fact, in my first incarnation I had exactly that problem:
when using atoi instead of atof,
if I input 3.2
it would perform the division on just the 3.

on the other hand, im finding the faq about fgets a bit confusing.
could someone provide me with an example in the context of this program?

thanks to everyone here for their help, as is very obvious, im just getting started...

3. Originally Posted by hebali
Prelude,
I see your point. I do want to avoid such inputs. In fact, in my first incarnation I had exactly that problem:
when using atoi instead of atof,
if I input 3.2
it would perform the division on just the 3.

on the other hand, im finding the faq about fgets a bit confusing.
could someone provide me with an example in the context of this program?

thanks to everyone here for their help, as is very obvious, im just getting started...
The FAQ is an example in the context of your program.

Code:
```char inXreal[8];

fgets(inXreal, 8, stdin); // first 8 characters of input are now here```

4. True, no argument with that. Just add error checking.

5. Atoi converts a string to an integer (no decimals, in other words).
Best use strtof or strtod.

6. aha! thanks. i get it now. sorry.

7. >could someone provide me with an example in the context of this program?
Sure:
Code:
```#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
char buffer[BUFSIZ];

printf ( "Enter a floating-point number: " );
fflush ( stdout );

if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
char *end;
double x;

errno = 0;
x = strtod ( buffer, &end );

if ( errno == ERANGE || end == buffer )
fputs ( "Invalid input\n", stderr );
else
printf ( "You entered &#37;f\n", x );
}

return 0;
}```
>Best use strtof or strtod.
Note that strtof is only available in C99 or as an extension.

8. Slinger: by error checking do you mean "if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';"
(as cited in the FAQ)?

Elysia, from the hmug site, I see the following about the use of srttof:

"strtof(const char *restrict nptr, char **restrict endptr);"
but I am not sure what the implementations of those: const char *restrict nptr, etc are

9. Is there a strtof actually? I can only see strotod.
But as for the question, the usage goes like this:
strotod(string_you_want_to_convert_here, [1]);
[1] This is a pointer to a pointer (char**) that recieves the address where strtod stopped parsing the string. So if you add junk "10d", it will assign to a pointer to address in the string where "d" resides, so you know that at this point, it stopped parsing.

You can just pass NULL if that was confusing.

10. >but I am not sure what the implementations of those: const char *restrict nptr, etc are
Don't worry about restrict for now. A lot of experienced C-ites have trouble with the concept, and it's still a very new feature. But to answer your question, the first parameter is the string with your value. The second parameter tells you how far into the string strtof got before it couldn't go on (you can pass NULL to this parameter if you don't care). For example:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
const char *p = "123.456test";
char *end;
double x;

printf ( "Converting \"&#37;s\"\n", p );
x = strtod ( p, &end );
printf ( "Found %f with \"%s\" left over\n", x, end );

return 0;
}```
>Is there a strtof actually?
In C99 or as an extension, yes. But prior to C99 you can't rely on it being supported.

11. Originally Posted by hebali
Slinger: by error checking do you mean "if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';"
(as cited in the FAQ)?

Elysia, from the hmug site, I see the following about the use of srttof:

"strtof(const char *restrict nptr, char **restrict endptr);"
but I am not sure what the implementations of those: const char *restrict nptr, etc are

Was not thinking specifics but, scanf does have its limits with user input. If you are needing to check that a number, ie a float, was truly entered then you will need to check that no letters were entered, perhaps by checking against, isalpha? But Preludes recommendations looks good and clean.

12. Problem is that it leaves crap in the input buffer which you need to clean (if it fails). That's why I like the fgets/strtod/strtol approach instead.

13. >That's why I like the fgets/strtod/strtol approach instead.
This has the same problem, though it's subtler. In practice, fgets is best used as the foundation of a more robust, but application-specific, line reading function.

14. Ok, so my current implementation looks like this:
Code:
```/* Variables */
float Xreal;
float Yreal;
float Zreal;
float Xforce;
float Yforce;
float Zforce;
float Tval;
char inXreal[8];
char inYreal[8];
char inZreal[8];
char inZforce[8];

/* Prompts */

/* Get coordinates of real-perspective point */
printf("Xreal value: ");
fgets(inXreal, 8, stdin);
Xreal=strtof(inXreal, NULL);

printf("Yreal value: ");
fgets(inYreal, 8, stdin);
Yreal=strtof(inYreal, NULL);

printf("Zreal value: ");
fgets(inZreal, 8, stdin);
Zreal=strtof(inZreal, NULL);

/* Get Z coordinate of forced-perspective point */
printf("Zforce value: ");
fgets(inZforce, 8, stdin);
Zforce=strtof(inZforce, NULL);

/* Computation */
Tval=Zforce/Zreal;
Xforce=Tval*Xreal;
Yforce=Tval*Yreal;

/* Output Display */
printf("The point: (&#37;.3f, %.3f, %.3f) was generated with a Tvalue of: %.3f.\n",Xforce,Yforce,Zforce,Tval);

return(0);
}```
It builds. But, I think I may be seeing the effect of using NULL?? I'm not sure. When I run it, if each of the user input are the same length: for example, 2.343 5.425, etc., then it will perform normally. however, if i make one of the inputs have a different number of characters (length?) from the previous, it will skip the next prompt, and go right to the final output with incorrect values.

What is this about?

Thanks. I must say, this is the most helpful forum I've ever used. I'm new to programming, but not new to computing (mainly Maya, etc.) and you have all been wonderful.

15. Everything has its ups and downs, but it's easier to check for errors and no need with annoying syntax to clear input buffers. IMHO, of course.

Popular pages Recent additions