0

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);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
HewadL
  • 1
  • 1
  • 2
    So do some basic debugging: what's the return value of scanf(), and what's the value of length? Also, two obvious bugs: 'if (length > 0 || length <= 100)' is always true. You probably mean &&. Also, you're printing the address of area (&area), when you really want its value. Drop the &. – Lee Daniel Crocker Nov 08 '17 at 18:13
  • Sorry, I got that part right after I posted but it still outputs as 0.0000, from what i've looked up online, its usually a type specifier error, but I dont see what mine is doing it? – HewadL Nov 08 '17 at 18:17

1 Answers1

1

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.

Phil Kiener
  • 778
  • 1
  • 7
  • 15