Hello -
I am working on one of my labs, but I am having difficulty with the results being inconsistent.
The passing by reference is working. I tested random data and I don't understand why some works and others produces incorrect results.
Data tested:
5, 9, 7 = perimeter of 21 and area of 12.247 (correct)
5, 6, 9 = perimeter of 20 and area of 14.142 (correct)
9, 7, 3 = perimeter of 19 (this part is correct) and area of 0.000 (incorrect)
5, 8, 12 = perimeter of 25 (this part is correct) and area of 0.000 (incorrect)
Line 57 & 58: Compiler gives a warning of " converting to int from double. I don't understand this, since I'm sure I used the correct data types.
The output should be precision of 4 significant digits, but only the "area" is converting correctly, and not the perimeter.
I appreciate any input you guys may have. I checked my codes several times and I just can't seem to figure out the cause of the problems.
Thank you. :'(
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
bool isValid (int tri1, int tri2, int tri3);
bool side;
void calc(int a, int b, int c, double& s, double& area);
int main ()
{
int side_a, side_b, side_c;
double s, area;
// Function prompting user for 3 numbers to be used for triangle sides
while (side != 1) //Establish loop here. If side not equal to 1, error encountered, try again.
{
cout << "Enter 3 lengths of the triangle sides: \n";
cout << " Side 1: "; cin >> side_a;
cout << " Side 2: "; cin >> side_b;
cout << " Side 3: "; cin >> side_c;
cout << endl;
/*Call isValid Function to determine if the user inputs are valid.
isValid Function needs to loop until the correct values are entered */
bool side = isValid (side_a, side_b, side_c);
{
if (side = 0)
{
cout << "These edges do not form a triangle." << endl;
side = 0; // Populate side with 0. Error encountered. Give it another try.
}
else // All ok? populate valid with 1, end the loop.
{
side = 1; // If sides = triangle, end loop and continue.
}
}
} // end loop.
/*Call calc Function - this function will return the value of perimeter and Area.
(these values were stored in the reference parameters S and Area. Output should be perimeter only (side a, b, c) and area. Set precision to 4 */
calc (side_a, side_b, side_c, s, area);
[B]return s * 2; // Line 57, warning converting to int from double
return area; // Line 58, warning converting to int from double[/B]
cout << fixed << setprecision (4);
cout << "Your triangle has a perimeter of " << s << " and an area of " << area << endl;
system ("pause");
return 0;
}//end main
// isValid Function
bool isValid (int tri1, int tri2, int tri3)
{
if ((tri1 + tri2 > tri3) && (tri1 + tri3 > tri2) && (tri2 + tri3 > tri1))
side = 1;
else
side = 0;
return side;
} // end isValid
/* Write a Function 'calc'. Return type = void, 5 parameters (a, b, c, &s, &area.
Calculate s = semiperimeter and area. Results of s and area are to be stored in the
reference parameters s and area */
void calc(int a, int b, int c, double& s, double& area)
{
s = ((a + b + c)/2);
area =(s-a)*(s-b)*(s-c);
area = area * s;
area = sqrt (area);
return;
} // end calc function