# C++, Function Calls, Recursive Functions



## C++ Beginner (Mar 16, 2004)

I have a really simple program for my intro to C++ class. The idea is to determine the area of a triangle given some information about it. One of the requirements is to check if the information given is valid and will create a triangle. Can anyone look it over and tell me what the problem is.? The comments make it seem a little cluttered.

My program calls the function Validity when a triangle cannot be made with the given information. Then, it returns to the main function and prompts the user again. If I put in valid information that does make a triangle this time around, my program outputs some garbage as the area. "The area of your triangle is -1.#IND."
It seems as though there is still information stuck in the buffer and it returns the area calculated using the invalid data.

My email address is [email protected]

Here is my source code. Thank you so much for whatever comments or advice you have to offer:

#include <iostream.h>
#include <math.h> // preprocessor directives
#include <stdlib.h>

const double PI = 3.14159;	// constant double variable declaration

double SSS();
double SAS();
double ASA();	// function prototypes
double SSA();
double AAS();
int Validity();

int main()
{
int answer;	// variable declarations
double area;

cout << "To calculate the area of your triangle, please select which of the following " << endl;
cout << "sets of information you have." << endl << endl;

cout << "1. Three sides of the triangle. (SSS)" << endl << endl;
cout << "2. Two sides and the angle between. (SAS)" << endl << endl;
cout << "3. Two angles and the side between. (ASA)" << endl << endl;	// menu
cout << "4. Two sides and an angle not between. (SSA)" << endl << endl;
cout << "5. Two angles and an opposite side. (AAS)" << endl << endl;
cout << "Please make selection now: ";
cin >> answer;

if(answer == 1)
{
area = SSS();
}
else if(answer == 2)	// series of if/ else if control structures that call
{ // functions to calculate triangle areas
area = SAS();
}
else if(answer == 3)
{
area = ASA();
}
else if(answer == 4)
{
area = SSA();
}
else
{
area = AAS();
}

system("CLS"); // clears screen
cout << "The area of your triangle is " << area << endl << endl;

return 0;
}

double SSS() // function to calculate area of a triangle given the lengths of the sides
{ // no parameters are passed to the function
double side1, side2, side3, sp, area;	// variable declarations

system("CLS"); // clears screen
cout << "Please enter side 1:"; // prompts user
cin >> side1;
cout << "Please enter side 2:";
cin >> side2;
cout << "Please enter side 3:";
cin >> side3;

if((side1 + side2 <= side3) || (side2 + side3 <=side1) || (side3 + side1 <= side2))
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}

sp = (side1 + side2 + side3) / 2;	// calculates semiperimeter, then area
area = sqrt(sp * (sp - side1) * (sp - side2) * (sp - side3));

return area;
}

double SAS()	// function to calculate area of a triangle given two sides and the angle between
{ // no parameters are passed to the function
double side1, side2, side3, angle3, sp, area;	// variable declarations

system("CLS"); // clears screen

cout << "Please enter side1: "; // prompts user
cin >> side1;
cout << "Please enter side2: ";
cin >> side2;
cout << "Please enter angle3 (between sides 1 and 2) in degrees: ";
cin >> angle3;

side3 = sqrt((side1 * side1) + (side2 * side2) - (2 * side1 * side2 * cos(angle3 * PI / 180)));

if(angle3 >= 180)
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}
if((side1 + side2 <= side3) || (side2 + side3 <=side1) || (side3 + side1 <= side2))	
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}

sp = (side1 + side2 + side3) / 2;	// calculates semiperimeter, then area
area = sqrt(sp * (sp - side1) * (sp - side2) * (sp - side3));

return area;
}

double ASA()	// function to calculate area of a triangle given two angles and the side between
{ // no parameters are passed to the function
double angle1, angle2, side3, angle3, side1, side2, sp, area;	// variable declarations

system("CLS"); // clears screen

cout << "Please enter angle1 in degrees: "; // prompts user
cin >> angle1;
cout << "Please enter angle2 in degrees: ";
cin >> angle2;
cout << "Please enter side3 (between angles 1 and 2): ";
cin >> side3;

angle3 = 180 - angle1 - angle2;

side2 = (side3 / sin(angle3 * PI / 180)) * sin(angle2 * PI / 180);

side1 = (side3 / sin(angle3 * PI / 180)) * sin(angle1 * PI / 180);

if(angle1 + angle2 >= 180)
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}
if((side1 + side2 <= side3) || (side2 + side3 <=side1) || (side3 + side1 <= side2))	
{ //checks validity of triangle sides.
Validity();	// function call to rerun program
}

sp = (side1 + side2 + side3) / 2;	// calculates semiperimeter, then area
area = sqrt(sp * (sp - side1) * (sp - side2) * (sp - side3));

return area;
}

double SSA()	// function to calculate area of a triangle given two sides and an angle opposite
{ // no parameters are passed to the function
double angle1, angle2, side3, angle3, side1, side2, sp, area;	// variable declarations

system("CLS"); // clears screen

cout << "Please enter side1: "; // prompts user
cin >> side1;
cout << "Please enter side2: ";
cin >> side2;
cout << "Please enter angle1 in degrees: ";
cin >> angle1;

angle2 = asin((side2 * sin(angle1 * PI / 180)) / side1) * 180 / PI;

angle3 = 180 - angle1 - angle2;

side3 = (side2 / sin(angle2 * PI / 180)) * sin(angle3 * PI / 180);

if(angle1 >= 180)
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}
if((side1 + side2 <= side3) || (side2 + side3 <= side1) || (side3 + side1 <= side2))
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}

sp = (side1 + side2 + side3) / 2;	// calculates semiperimeter, then area
area = sqrt(sp * (sp - side1) * (sp - side2) * (sp - side3));

return area;
}

double AAS()	// function to calculate area of a triangle given two angles and a side opposite
{ // no parameters are passed to the function
double angle1, angle2, side3, angle3, side1, side2, sp, area;	// variable declarations

system("CLS"); // clears screen

cout << "Please enter angle1 in degrees: "; // prompts user
cin >> angle1;
cout << "Please enter angle2 in degrees: ";
cin >> angle2;
cout << "Please enter side1: ";
cin >> side1;

side2 = (side1 / sin(angle1 * PI / 180)) * sin(angle2 * PI / 180);

angle3 = 180 - angle1 - angle2;

side3 = (side1 / sin(angle1 * PI / 180)) * sin(angle3 * PI / 180);

if(angle1 + angle2 >= 180)
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}
if((side1 + side2 <= side3) || (side2 + side3 <= side1) || (side3 + side1 <= side2))
{	// checks validity; to see if triangle can be made
Validity();	// function call to rerun program
}

sp = (side1 + side2 + side3) / 2;	// calculates semiperimeter, then area
area = sqrt(sp * (sp - side1) * (sp - side2) * (sp - side3));

return area;
}

int Validity() // function that allows the user to make another calculation without exiting the program
{ // no parameters are passed to the function

cout << endl;
cout << "The information given does not produce a valid triangle." << endl << endl;	// error message

main();

return 0;
}


----------



## AlbertB (Nov 24, 2002)

Let us assume we are going to enter 3 sides of a triangle.

If you think of what will happen with your recursion in place. Your first call to main calls SSS(). This accepts the 3 values, checks out their validity and calls validity() if it finds them invalid. Validity() then makes a second call to main(). However that set of incorrect data is left on the stack as the current parameters of that first call to main() and as your recursion eventually unwinds the program will try to resolve that call with those parameters.

Look at where your return from your successful second call will bring you - back into the first SSS() function just after the call to validity(). It will then still try to complete that iteration with your incorrect data.

As a quick fix, simply reset your side1, side2, and side3 variables to 0.0 immediately after the call to validity() and still inside the if()in the SSS() function and run your program with an incorrect SSS triangle then a correct one. You will now see that your unwanted message reports that final extra area as 0.

That explains the problem I think, and you seem easily competent enough to get rid of that unwanted message if it should be necessary. 

One tip for posting here, as you prepare your post place your code inside the two tags [ code ] [ /code ], (without the spaces I have had to put in these examples to stop them from being parsed out)!  This will allow you to display your code in a plainer font with its normal indentation, (use spaces rather than a tab), and makes it easier for you (and me  ) to pick out and follow.


----------



## C++ Beginner (Mar 16, 2004)

AlbertB...Thank you for the help and the advice on posting in the future. My program works well now!

Chris


----------

