# A basic math programming question

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 02-25-2008
hebali
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[8];
char inYreal[8];
char inZreal[8];
char inZforce[8];

/* 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!
• 02-25-2008
hebali
by the way
by the way:
I am working in Xcode 3 on a mac running os x leopard
• 02-25-2008
tabstop
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.)
• 02-25-2008
Prelude
>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.
• 02-25-2008
hebali
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.

• 02-25-2008
hebali
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?
• 02-25-2008
hebali
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
• 02-25-2008
vart
you can use fgets(buffer, sizeof buffer, stdin)
• 02-25-2008
Prelude
>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 ) {   /* Your code here */   return 0; }```
• 02-25-2008
hebali
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"
• 02-25-2008
tabstop
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".
• 02-25-2008
hebali
yes, but how do i implement that?
• 02-25-2008
vart
Once again - have you read the FAQ?
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
• 02-25-2008
slingerland3g
I would use scanf

Code:

```int main() {     float a;     float b;         printf("enter a float:\n");     scanf("&#37;f", &a);         printf("Your float is %.3f", a);         return (EXIT_SUCCESS);        }```
• 02-25-2008
Prelude
>I would use scanf
scanf has its own problems, and conveniently enough, your example exhibits one of them. What happens if I type "I'm a little teapot" instead of "123.456"?
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last