Thread: Problem with if statements in the second half of the code. See comments for problem.

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    1

    Problem with if statements in the second half of the code. See comments for problem.

    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;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please explain why you think you code will ever have both "Rec_area>Tri_area" and "Rec_area<Tri_area" true.

    NOTE: Nesting the second if inside the first is wrong. You likely need an else if instead of the second if.

    Edit: Add link If Statements in C - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    For starters change the first line to:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void){
    ...
    then link in the math library. For gcc add "-lm" to link in the math library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comments problem with MSVC 2003
    By mikahell in forum Tech Board
    Replies: 7
    Last Post: 09-04-2006, 06:04 PM
  2. Problem parsing comments and such in text file
    By zaxxon in forum C Programming
    Replies: 3
    Last Post: 08-09-2004, 12:14 AM
  3. Half life Problem which I am right and the teacher is wrong
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 11-06-2002, 04:28 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Half Life Blue Shift Problem
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-23-2002, 09:57 AM

Tags for this Thread