Thread: Help! What is wrong with this program?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Help! What is wrong with this program?

    EDIT: I figured it out. This can be deleted.

    My compiler does not like line 38: p = per(a, b, c);

    BTW, this program is supposed to find the area and perimeter of a right triangle. If someone could test it if they know what is wrong and make sure it works, I would appreciate it as my compiler never tells me the final "answer" or whatever even when I use getchar(). If you could help me, I would appreciate it. I cannot for the life of me figure out what is wrong with it.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float areaTri(float a, float b)
    {
          float area;
          area = .5 * a * b;
          return area;
    }
    
    float hypotenuse(float a, float b)
    {
          float c;
          c = sqrt (a*a + b*b);
          return c;
    }
    
    float per(float a, float b, float c)
    {
          float p;
          p = a + b + c;
          return p;
    }
    
    void triangle()
    {
         float a, b, c, area;
         printf("Please enter the length of the base of the triangle: ");
         scanf("&#37;f%", &a);
         fflush(stdin);
         
         printf("Please enter the heighth of the triangle: ");
         scanf("%f", &b);
         fflush(stdin);
         
         area = areaTri(a, b);
         c = hypotenuse(a, b);
         p = per(a, b, c);
         
         printf("The area of the triangle is: %6.1f\n", area);
         printf("Side c of this triangle is %6.1f\n", c);
         printf("The perimeter of this triangle is %6.1f\n", p);
    }
    
    int main()
    {
        triangle();
        
        return 0;
    }
    Last edited by Northstar; 11-04-2007 at 10:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It might "work" for you, but those fflush(stdin) calls mean it's still broken.
    See the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM