# Thread: A basic math programming question

1. ## A basic math programming question

Hi,
I'm trying to learn some basic ideas about programming mathematical functions in C.
I would like the user to be able to input a number with decimals, for instance "2.123"
and then use this input to carry out an equation.
Here is how I've been doing it:
int main (int argc, char * const argv[]) {

/* Variables */
float Xreal;
float Yreal;
float Zreal;
float Xforce;
float Yforce;
float Zforce;
float Tval;
char inXreal;
char inYreal;
char inZreal;
char inZforce;

/* Prompts */

/* Get coordinates of real-perspective point */
printf("Xreal value: \n");
gets(inXreal);
Xreal=atoi(inXreal);

printf("Yreal value: \n");
gets(inYreal);
Yreal=atoi(inYreal);

printf("Zreal value: \n");
gets(inZreal);
Zreal=atoi(inZreal);

/* Get Z coordinate of forced-perspective point */
printf("Zforce value: \n");
gets(inZforce);
Zforce=atoi(inZforce);

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

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

This works so long as the input values are whole numbers.
But I would also like to be able to perform these functions using decimals.
I understand that the problem must have something to do with using CHAR and ATOI, but I'm not certain of another way to get user input for decimals, etc.

Thanks! 2. ## by the way

by the way:
I am working in Xcode 3 on a mac running os x leopard 3. Since atoi stands for "alphanumeric to integer", it seems unlikely it will work for getting floating point values.

Whatever path you're following in your quest for C knowledge, if it took you past "atoi" it should have taken you past "atof" as well. (And "atof" stands for "alphanumeric to floating point" -- it returns a value of type double.) 4. >int main (int argc, char * const argv[]) {
If you're not going to use the parameters, don't include them. Also, be sure to return a value from main, even if it's always 0:
Code:
```int main ( void )
{
return 0;
}```
>gets(inXreal);
Never use gets. It's an unsafe function, and it's impossible to make gets safe.

>Xreal=atoi(inXreal);
Even if you use it with integers, atoi is generally a bad idea unless you've validated the string first. I'd recommend strtol, or in the case of floating-point values, strtod. 5. Hi, thanks for your very quick response.
I suspected that was exactly the problem, and so I checked the index of the book I am using for other ato... functions, but believe it or not, they do not mention atof. 6. prelude,
can you clarify:
I am getting a message in terminal that gets is unsafe...
How could I perform that same function without it?

Printf("Input number: \n");
WHAT GOES HERE? 7. and, in the context of the above code... where do i put the "return", because my "{" then contains the rest of the program, im not sure where the return zero goes

im just getting started you can use fgets(buffer, sizeof buffer, stdin) 9. >How could I perform that same function without it?
Sorry about that. I was in so much of a hurry that I forgot to point you toward the fgets function.

>where do i put the "return"
Put the return in main when you're done. A good start is right at the end, just before the closing brace.
Code:
```int main ( void )
{
return 0;
}``` 10. ok, i get the return. thanks

but, im not certain i understand the fgets.
if it is "fgets(buffer, sizeof buffer, stdin)"
what do I put for each: buffer, size of, stdin?

all i want is to have it understand an input such as "2.123" 11. buffer = where you want the answer to go.

sizeof buffer = size of the buffer. IOW, how many characters are in your char array? (Answer: 8.)

stdin = stdin. You know, the thing where people type, and it shows up on the screen. Short for "standard in". This should literally be the word "stdin". 12. yes, but how do i implement that? 13. Once again - have you read the FAQ?
http://faq.cprogramming.com/cgi-bin/...&id=1043284385 14. I would use scanf

Code:
```int main()
{
float a;
float b;

printf("enter a float:\n");
scanf("&#37;f", &a);  Popular pages Recent additions 