Using overloaded functions

So far this is my code; I'm supposed to write a program with two overloaded functions both named triangleArea. In main, randomly ask the user to enter either base and height or the lengths of the three sides and then make the appropriate call to triangleArea. triangleArea should return the area to main where it will be displayed.

code
{
#include <iostream>
using namespace std;

// function prototype
double triangleArea(int base, int height); // Calculates area when given base and height

double triangleArea(int length1, int length2, int length3); // Calculates area when given 3 side lengths

int main()
{
int height;
int base;
char choice;
int length1;
int length2;
int length3;

do
{
cout << "Enter base and height of a triangle " << endl;
cin >> base >> height;
triangleArea(base, height);

cout << "Run again? (y/n) " << endl;
cin >> choice;

cout << "Enter the lengths of the sides of the triangle " << endl;
cin >> length1 >> length2 >> length3;
triangleArea(length1, length2, length3);


}
while (choice == 'y');
cout << "Goodbye! " << endl;



}

double triangleArea(int base, int height)
{
int ba;
int he;
double area = (ba * he) * (0.5);
return area;
}

double triangleArea(int length1, int length2, int length3)
{
int length1;
int length2;
int length3;
double total = (length1 + length2 + length3) / 2;
return total;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cmath> // sqrt function

using namespace std;

// function prototype
double triangleArea(double, double); // Calculates area when given base and height
double triangleArea(double, double, double); // Calculates area when given 3 side lengths

int main()
{
    double height = 0.0;
    double base = 0.0;
    char choice;
    double length1 = 0.0;
    double length2 = 0.0;
    double length3 = 0.0;
    
    do{
        
        cout << "Enter base and height of a triangle " << endl;
        cin >> base >> height;
        cout << triangleArea(base, height) << '\n';
        
        cout << "Run again? (y/n) " << endl;
        cin >> choice;
        
        cout << "Enter the lengths of the sides of the triangle " << endl;
        cin >> length1 >> length2 >> length3;
        cout << triangleArea(length1, length2, length3) << '\n';
        
        
    }while (choice == 'y');
    
    cout << "Goodbye! " << endl;
    
    return 0;
}

double triangleArea(double aBase, double aHeight){
    
    // Pythagoras to get 3rd side
    double hypotenuse = sqrt( aBase * aBase + aHeight * aHeight);
    return triangleArea(aBase, aHeight, hypotenuse); // Uses 3-side equation
}

double triangleArea(double L1, double L2, double L3){
    
    double p = (L1 + L2 + L3)/2;
    // Heron's formula
    double area = sqrt( p * (p - L1) * (p - L2) * (p - L3) );
    
    return area;
}
Last edited on
Topic archived. No new replies allowed.