# Thread: Trying to do basic math functions. Why doesn't this work?

1. ## Trying to do basic math functions. Why doesn't this work?

Hello,
I'm trying to write a simple program that calculates projectile distance and time. I was given the basic equations but I'm writing something incorrectly because my output is currently zero. I have a feeling this may be an explicit type casting issue but I'm not sure what to do:
Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
const double PI = 3.14, GRAVITY = 9.8;
double degrees = 0, velocity = 0, angle = 0, range = 0, time = 0 ;

printf("Enter the measure of the angle in degrees. \n");
scanf("%d", &angle);

printf("Enter the initial velocity. \n");
scanf("%d", &velocity);

angle = angle * (PI/180.0);
range = (sin(2 * angle)) * pow(velocity, 2)/ GRAVITY;
time  = range/(velocity * (cos(angle)));

printf("The projectile will travel %d meters.\n", range);
printf("It will take %d seconds.\n", time);

return 0;
}```
Can anyone offer hints as to how I should fix this?

crazychile

2. Do not use &#37;d to read a double.
I believe the correct is %lf (but check the docs to be sure).
And don't print doubles using %d; use %f.

It's YOUR responsibility to tell scanf/printf/etc the correct type you are passing to it (this can be avoided in C++, but not C). If you do not, you will get unexpected results or crashes. Or simply put, undefined behavior.

3. To clarify, the program compiles but the output values for the print statements are each zero.
Thank you,
crazychile

4. Yes, I'm sure it does, but this is a runtime error, not a compile error.

5. ALL HAIL ELYSIA!!!!

That worked!
Thanks,
crazychile

6. Depending on which compiler you are using, enabling warnings may actually tell you that the format for the printf and scanf are wrong. It would be a warning, but it's still a lot more help than trying to figure out yourself what you got wrong.

--
Mats

Popular pages Recent additions