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";
}

Recommended Answers

All 8 Replies

where i want the result to be exactly as if i write the program this way

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	cout << "ANGLES OF A TRIANGLE\n\n";
	
	double a;
	double b;
	double c;
	const double PI = 4.0 * atan(1.0); // accurate value for pi


	cout << "Enter side 1 : ";
	cin >> a;

	while ( a <= 0 )
	{
		cout << "The length of a side must be positive\nEnter side 1 : ";
		cin >> a;
	}
	
	cout << "Enter side 2 : ";
	cin >> b;

	while ( b <= 0)
	{
		cout << "the length of a side must be positive\nEnter side 2 : ";
		cin >> b;
	}

	if ( (a-b) < 0)
	{
	    cout << "Side 3 must lie between  " << (b-a) << " and " << (a+b) << ".\nEnter side 3 : ";
	    cin >> c;
	}
	else
	{
		cout << "Side 3 must lie between  " << (a-b) << " and " << (a+b) << ".\nEnter side 3 : ";
	    cin >> c;
	}

	while ( c <= (a-b) || c >= (a+b) )
	{
		cout << "that value will not make a triangle\nEnter side 3 : ";
		cin >> c;
	}

	double A;
	double B;
	double C;

	A = acos ( ( ( b*b ) + ( c*c ) - ( a*a ) ) / ( 2*b*c ) ) * ( 180 / PI ); 
	B = acos ( ( ( a*a ) + ( c*c ) - ( b*b ) ) / ( 2*a*c ) ) * ( 180 / PI );
	C = 180 - (A+B);

	cout << "The angle opposite to the side of lenght " << a << " is " << A <<" degrees.\n";
	cout << "The angle opposite to the side of lenght " << b << " is " << B <<" degrees.\n";
	cout << "The angle opposite to the side of lenght " << c << " is " << C <<" degrees.\n";
	cout << "\nEND OF PROGRAM\n\n";

	return 0;

}

You'll have to store the return value. Right now, the return values are all being discarded. To store the return, declare a variable of the same type as the return, then use the assignment operator with a function call on the right of the assignment.

#include <iostream>
using std::cout;
using std::endl;

double someFunc();  //function prototype, indicating a double return

int main() {
  double functionReturn = 0.0;  //declare a local double

  cout << "The value of functionReturn is " << functionReturn << endl;

  functionReturn = someFunc();  //call someFunc(), store return to local double

  cout << "The value of functionReturn is now " << functionReturn << endl;

  return 0;
}

double someFunc() {
  cout << "In someFunc..." << endl;
  double aValue = 3.1416;
  return aValue;
}

how to do so ... in the example you did, the value is pre-entered at the stage of writing the code... i want to use the value entered while using the program ... lets say that a function is meant to enter a value "cin >> value;" .... another function to recall the first function twice so you have two values ... and a third function that adds these two values whitout entering the values many times... what happenes in my program is that it asks me to enter the number then the second one then because of the mistake that i want a solution for it asks me to enter these values again...

Think a little. Don't knee-jerk and say "that's not what I need". It's not a big stretch to make it accept user input instead. Your existing code already handles that. You just need to use the returned value properly.

However, based on what I'm seeing, you may need to change the definition of get_sides(). You can only return 1 value from a function, you're trying to get back more than 1.

Your textbook should have information on return values and passing conventions. I suggest you read up a little.

i would really appreciate it if you can write the adding program i used as an example in my last post ... thank you

I'm not going to do that. You're showing some effort, which is good. You don't get an easy out just because you're trying though. We'll talk you through your algorithms and make suggestions for changes, but we won't do the work for you.

If your instructor has given you sufficient leeway, I would consider renaming get_sides() to get_side() and making some logic changes to it. Then, you call this new get_side() function once for each side you need to input, making sure to properly store the return values. Then, you would use these stored return values as the arguments for your other function calls.

#include <iostream>
using std::cout;
using std::endl;

double someFunc();
void anotherFunc(double, double);

int main() {
  double functionReturn1 = 0.0, functionReturn2 = 0.0;  //declare local doubles

  functionReturn1 = someFunc();  //call someFunc() once for each local double
  functionReturn2 = someFunc();

  anotherFunc(functionReturn1, functionReturn2);  //call anotherFunc(), provide functionReturns as arguments

  cout << "The value of functionReturn is now " << functionReturn << endl;

  return 0;
}

double someFunc() {
  cout << "In someFunc..." << endl;
  double aValue = 3.1416;
  return aValue;
}

void anotherFunc(double arg1, double arg2) {
  /* ... do something ... */
}

thank you anyways

really thank you ... finally made it ...

#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);
double third_side(double,double);
double cosine(double ,double ,double);
void end_title (void);

int main()
{	
	title();
	double x = 0.0, y = 0.0, z = 0.0;
	x = get_sides(1);
	y = get_sides(2);
	z = third_side(x,y);
	cosine(x,y,z);
	cosine(y,x,z);
	cosine(z,y,x);
	end_title();
	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,double side_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 (double a, double b, double c)
{	
	
	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\n";
}

void end_title (void)
{
	cout << "\n\nEND OF PROGRAM\n";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.