I need to find the area of the octagon using the current functions I have and with the same parameters, every time I input something it just outputs as "0.000000," how do I fix this? It is also not error checking properly.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int die(const char * msg);
double areaFromSide(double length);
int main()
{
double length = 0;
printf("Enter length of one side of the octagon: ");
scanf("%f", &length);
if (length > 0 && length <= 100)
{
double area = areaFromSide(length);
printf("Area of the octagon is: %f\n", area);
}
else
{
die("Input Failure!");
}
return 0;
}
double areaFromSide(double length)
{
double area = 0;
area = 2 * (1 + (sqrt(2)))*pow(length, 2);
return area;
}
int die(const char * msg)
{
printf("Fatal error: %s\n", msg);
exit(EXIT_FAILURE);
}
You're always getting an output of 0.000000
because your scanf()
call is wrong; to read a double, you need to use %lf
instead of just %f
(see this question for more on that).
Also, in printf()
, you want to print the value of the area, not its address (which is retrieved by prefixing a variable with &
) - just use
printf("Area of the octagon is: %f", area);
Also, regarding error catching, you have (length > 0 || length <= 100)
as predicate. This will allow any value which is not negative, since ||
needs only one side to evaluate to true. You want to use
(length > 0 && length <= 100)
to make sure the length is both larger than 0 and smaller than 100.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.