Passing by Reference

I'm writing a program that asks the user for 3 sides. Makes sure it could be a triangle, and then uses equations to figure out the area and perimeter of the triangle. The program should only use 3 functions.

For whatever reason, my code won't run. I know I'm close but there is something that is least bit off.

#include<iostream>
#include<cmath>
using namespace std;

bool valid(int side1, int side2, int side3);
void semi_perimeter(int side1, int side2, int side3, double& semi);
void get_area(int side1, int side2, int side3, double& area, double& perimeter);


int main()
{
int one_side, two_side, three_side;
double area, perimeter, semi;
bool isvalid;
char ans;

do{

do{

do{
cout << "\nEnter the length of the first side (whole #): ";
cin >> one_side;

if(one_side < 0)
cout << "\nThat is invalid input. Try again.\n";
}while(one_side < 0);

do{
cout << "\nEnter the length of the second side (whole #): ";
cin >> two_side;

if(two_side < 0)
cout << "\nThat is invalid input. Try again.\n";
}while(two_side < 0);

do{
cout << "\nEnter the length of the third side (whole #): ";
cin >> three_side;

if(three_side < 0)
cout << "\nThat is invalid input. Try again.\n";
}while(three_side < 0);

isvalid = valid(one_side, two_side, three_side);

}while(!(isvalid));


cout << "\nA triangle with sides of " << one_side << ", " << two_side
<< " and " << three_side
<< " has a perimeter of " << get_area(one_side, two_side, three_side, area, perimeter)
<< " and an area of " << get_area(one_side, two_side, three_side, area, perimeter);

cout << "\nWould you like to run the program again? (y or n): ";
cin >> ans;

}while(ans == 'Y' || ans == 'y');

return 0;
}


bool valid(int side1, int side2, int side3)
{
if((side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1))

return false;
}


void semi_perimeter(int side1, int side2, int side3, double& semi);
{
semi = (static_cast<double>(side1) + static_cast<double>(side2) + static_cast<double>(side3)) / 2;
}


void get_area(int side1, int side2, int side3, double& area, double& perimeter);
{
double semi_p;

semi_perimeter(side1, side2, side3, semi_p)

area = sqrt((semi_p)*(semi_p - static_cast<double>(side1))*(semi_p - static_cast<double>(side2))*(semi_p - static_cast<double>(side3)));

perimeter = side1 + side2 + side3;
}
1. function definitions have semicolon which should be removed. it should be like this.

1
2
3
4
void semi_perimeter(int side1, int side2, int side3, double& semi)
{
semi = (static_cast<double>(side1) + static_cast<double>(side2) + static_cast<double>(side3)) / 2;
}

2. when you are trying to print, get_area returns nothing (void) which will give error.
<< get_area(one_side, two_side, three_side, area, perimeter);

your area and parameter functions should takes input as sides and should return the area.
Last edited on
Topic archived. No new replies allowed.