hi,
how can i use the result of a function without having it repeated ?
like in the code down there i have to use the result of the function get_sides() in all of the other functions..but the way i coded it down there will make me enter the values many times..the instructio is to create a program using functions only where i am supposed to have nothing in int main() except calling the functions get_sides twice, third_side once and cosine three times..
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 4.0 * atan(1.0); // accurate value for pi
void title(void);
double get_sides(int num);
double third_side();
double cosine(int num);
int main()
{
title();
get_sides(1);
get_sides(2);
third_side();
cosine(1);
cosine(2);
cosine(3);
return 0;
}
double get_sides(int num)
{
cout <<"Enter side " << num << " : ";
double side;
cin >> side;
while (side <= 0)
{
cout << " must be positive!!\nEnter side " << num <<" : ";
cin >> side;
}
return side;
}
double third_side()
{
double side_1 = get_sides(1);
double side_2 = get_sides(2);
double sum = (side_1) + (side_2);
double diff = (side_1) - (side_2);
double side;
if (diff < 0)
{
cout << " third side must be between " << (-1 * diff) << " and " << sum <<"\nEnter side 3 : ";
cin >> side;
while (side <= (-1*diff) || side >= sum)
{
cout << "this length will not make a triangle\nEnter side 3 : ";
cin >> side;
}
}
else
{
cout << " third side must be between " << diff << " and " << sum <<"\nEnter side 3 : ";
cin >> side;
while (side <= diff || side >= sum)
{
cout << "this length will not make a triangle\nEnter side 3 : ";
cin >> side;
}
}
return side;
}
double cosine (int num)
{
double a;
double b;
double c;
if (num = 1)
{
a = get_sides(1);
b = get_sides(2);
c = third_side();
}
if (num = 2)
{
b = get_sides(1);
a = get_sides(2);
c = third_side();
}
else
{
a = third_side();
b = get_sides(2);
c = get_sides(1);
}
double A;
A = acos ( ( ( b*b ) + ( c*c ) - ( a*a ) ) / ( 2*b*c ) ) * ( 180 / PI );
cout << "the angle in front of the side of length " << a << " is " << A << " degrees.\n";
return A;
}
void title (void)
{
cout << "angles of a triangle\n";
}