I am having a problem with the part of my code which compares the rectangle area with the triangle area. I cannot get the if else statements to run. The program terminates before the validation takes place. Im not sure what I am doing wrong, or if I must place the statement in another location.
Code:
Int main(void){
printf("Peter Donchik\n");
double Tri_side_A;
double Tri_side_B;
double Tri_side_C;
double Tri_length;
double Tri_width;
double Tri_area;
double Tri_perimeter;
double Tri_sides;
double Rec_width;
double Rec_length;
double Rec_area;
printf("What is the length of side A on the triangle?\n");
scanf("%lf", &Tri_side_A);
if((Tri_side_A >=1) && (Tri_side_A <= 100))
{
printf("What is the length of side B on the triangle?\n");
scanf("%lf", &Tri_side_B);
if((Tri_side_B >= 1) && (Tri_side_B <= 100))
{
printf("What is the length of side C on the triangle?\n");
scanf("%lf", &Tri_side_C);
if((Tri_side_C >= 1) && (Tri_side_C <= 100))
{
if((Tri_side_A < (Tri_side_B + Tri_side_C)) && (Tri_side_B < (Tri_side_C + Tri_side_A)) && (Tri_side_C < (Tri_side_A + Tri_side_B)))
{
Tri_perimeter=Tri_side_A+Tri_side_B+Tri_side_C;
Tri_sides=(Tri_perimeter/2);
Tri_area=sqrt(Tri_sides*(Tri_sides - Tri_side_A)*(Tri_sides - Tri_side_B)*(Tri_sides - Tri_side_C));
printf("The perimeter of the triangle is %.1f\n", Tri_perimeter);
printf("The area of the triangle is %.1f\n", Tri_area);
}
else
{
printf("The values entered do not make a valid triangle\n");
}
}
else
{
printf("The value for side C is out of range\n");
}
}
else
{
printf("The value for side B is out of range\n");
}
}
else
{
printf("The value for side A is out of range\n");
}
printf("What is the length of the rectangle?\n");
scanf("%lf", &Rec_length);
printf("What is the width of the rectangle?\n");
scanf("%lf", &Rec_width);
Rec_area=(Rec_length*Rec_width);
printf("The area of the rectangle is %.1f.\n",Rec_area);
//this works fine//
/*Here is where my code terminates.i cannot figure out how to have the program run the following
if statements to compare the two areas and tell which is greater.*/
if(Rec_area>Tri_area)
{
printf("The rectangle has a greater area.\n");
if(Rec_area<Tri_area)
{
printf("The Triangle has a greater area.\n");
}
else
printf("The area of both the triangle and rectangle are the same.\n");
}
return 0;
}