function to compute right triangle data

This is a discussion on function to compute right triangle data within the C++ Programming forums, part of the General Programming Boards category; I am stuck on this problem of computing the area and perimeter of a right triangle Can someone help please ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    Cool function to compute right triangle data

    I am stuck on this problem of computing the area and perimeter of a right triangle Can someone help please to tell me where I have gone wrong[


    CODE][/CODE]



    // Compute area and perimeter of right triangle
    #include <iostream>
    #include <cctype>
    #include <cmath>
    using namespace std;

    double right_tri_area (double base, double height);
    // Area formule

    double right_tri_perimeter (double a, double b, double h);
    // Perimeter formula

    int main()
    {
    double base, height,
    a, b, h, perimeter,
    area;
    cout << "Enter base and height of the triangle: ";
    cin >> base >> height;
    area = right_tri_area(base,height);
    cout << "Area = " << area << endl;
    cout << "Enter the two sides'lengths of sides anhd base of the triangle: ";
    cin >> a >> b >> h;
    cout << "Perimeter = " << right_tri_perimeter(a, b, h) << endl;
    system("pause");
    return 0;
    }

    double right_tri_area (int base, int height)
    { right_tri_area = (base * height)/2; // system("pause");
    return 0; }

    double right_tri_perimeter (int a, int b, int h)
    { right_tri_perimeter = (a + b + h);
    // system("pause");
    return 0; }



  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Hey dude, just to let you know the [/code] tag goes after your code

    Try this:
    Code:
    // Compute area and perimeter of right triangle
    #include <iostream>
    #include <cctype>
    #include <cmath>
    using namespace std;
    
    double right_tri_area (double base, double height) { return (base * height)/2; }
    // Area formule
    
    double right_tri_perimeter (double a, double b, double h) { return (a + b + h); }
    // Perimeter formula
    
    int main()
    {
    double base, height,
    a, b, h, perimeter,
    area;
    cout << "Enter base and height of the triangle: ";
    cin >> base >> height;
    area = right_tri_area(base,height);
    cout << "Area = " << area << endl;
    cout << "Enter the two sides'lengths of sides anhd base of the triangle: ";
    cin >> a >> b >> h;
    cout << "Perimeter = " << right_tri_perimeter(a, b, h) << endl;
    system("pause");
    return 0;
    }
    [edit]Your problem was here:
    Code:
    double right_tri_area (int base, int height)
    { right_tri_area = (base * height)/2; // system("pause");
    return 0; }
    
    double right_tri_perimeter (int a, int b, int h)
    { right_tri_perimeter = (a + b + h);
    // system("pause");
    return 0; }
    You don't set the function value, you want to be returning a value from the function instead. Apart from that your program was fine, but you may want to get int the habit of indenting. [/edit]
    Last edited by mike_g; 06-17-2007 at 09:56 AM.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Thank you thank you and thank you Can you help with the other posting about changing it to an input file--let me post it new

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    No need to post it again, I'll have a look for it.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    There were some changes so I redid it under classify number to file

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Your right about the indenting I have not made the program my file because of no documentation either

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21