Hi. I can't a small part of this program to work correctly; the square root function either returns a bunch of funny symbols OR it returns 0.00
On a side note, I have 2 questions:
1) If you have a structure nesting two other data structure types (like in my program where the structure contains an enumerated type as well as a union type) and you send this structure to a function, how do you write values directly into each of the fields via scanf?
2) Did I use the scanf and switch statements for the enumerated types correctly? I think I may have gotten that part to work on accident -.-
edit: forgot to add that the area for a triangle (according to this book) is sqrt(t(t - side1) * t(t - side2) * t(t - side3)) where t = (side1 + side2 + side3) / 2.
#include <stdio.h>
#include <math.h>
enum FIGURE {
RECTANGLE = 1, CIRCLE, TRIANGLE
};
typedef union {
double comp[3];
} DIM;
typedef struct {
FIGURE shape;
DIM size;
} DATA;
void get_area(DATA );
int main(void) {
DATA input;
while (input.shape != 4) {
printf("*** Menu ***\n");
printf("1: Rectangle\n");
printf("2: Circle\n");
printf("3: Triangle\n");
printf("4: Quit\n");
printf("Enter a selection: ");
scanf("%d", &input.shape);
switch(input.shape) {
case RECTANGLE:
printf("rectangle\n");
printf("Enter the length: ");
scanf("%lf", &input.size.comp[0]);
printf("Enter the width: ");
scanf("%lf", &input.size.comp[1]);
get_area(input);
break;
case CIRCLE:
printf("circle\n");
printf("Enter the diameter: ");
scanf("%lf", &input.size.comp[0]);
get_area(input);
break;
case TRIANGLE:
printf("triangle\n");
printf("Enter length of side1: ");
scanf("%lf", &input.size.comp[0]);
printf("Enter length of side2: ");
scanf("%lf", &input.size.comp[1]);
printf("Enter length of side3: ");
scanf("%lf", &input.size.comp[2]);
get_area(input);
break;
case 4:
printf("Have a nice day!\n");
break;
default:
printf("Not a selection!\n");
}
printf("\n");
}
return 0;
}
void get_area(DATA input) {
double area;
switch(input.shape) {
case RECTANGLE:
area = input.size.comp[0] * input.size.comp[1];
printf("Area = %.2lf\n", area);
break;
case CIRCLE:
area = (input.size.comp[0] / 2) * (input.size.comp[0] / 2) * 3.14;
printf("Area = %.2lf\n", area);
break;
case TRIANGLE:
double t = (input.size.comp[0] + input.size.comp[1] + input.size.comp[2]) / 2;
area = sqrt(t * ((t - input.size.comp[0]) * (t - input.size.comp[1]) *
(t - input.size.comp[2])));
printf("Area = %.2lf\n", area);
break;
}
}