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``````

one thing i don't understand is why in line 57 and 58 you have two returns if your main function is not over...

the errors you get are because main is declared as an int function, and the values you are returning are invalid conversions from double to int...

in your `void calc()` you should just use a, b, and c to calculate your perimeter and your area, because those operations there are messing your whole operations... maybe you should want to try this:

``````void calc(int a, int b, int c, double &s, double &area){
s=((a+b+c)/2);
area=sqrt (((s-a)*(s-b)*(s-c))*s);
}``````

try it and let me know...

Nichito is right about lines 57 & 58 - they have no place in this program, they do nothing useful other than cause it to end before displaying your results. His revision to the calc function body does not change anything, just compresses three lines into one. Saves a couple assignment operations, that's all.

As to the erroneous area calculations, step through the function using the problem values (9,7,3). You should always solve by hand using the algorithm you implement to verify it. In this case, you'll find that your 's' value is 9. What's the value, then, of s-a? What happens when you use that result in the multiplications?

Your code is not giving the correct perimeter. It's giving 1/2 of the perimeter. You need to store the perimeter separately from the 's' value you use in calculations.

And lastly, the value you calculate for 's' is not correct, in most cases. Remember that dividing integers gives only an integer result, so 9/2 is the same as 8/2. You're storing to a double, so do the arithmetic as a double, divide by 2.0.

:) My codes works great . No more errors. Thank you guys for all the suggestions.

I removed line 57 & 58. I thought that I had to do this to pass the value, but what I needed to do was just call my calc function. I agree with the revision to the calc body function. The original equation given to the class was: area = sqrt (s*(s-a)*(s-b)*(s-c)); I originally had this as one line, but changed it later. I agree that one line is better than three lines and much neater too.

I couldn’t agree more about solving the problems by hand first. I had actually done this for test data 5, 8, 12 (see below)

5 + 8+ 12 = 25.00 perimeter
25 /2.0 = 12.50
12.50 – 5 = 7.50
12.50 – 8 = 4.50
12.50 – 12 =.50
7.50 * 4.50 * .50 = 16.88
12.50 * 16.88 = 211
sqrt (211) = 14.525839 area

I changed the code to divide by 2.0 (I always forget to do double). Lastly, I created a new variable ‘perimeter’ to store the perimeter value. And result of ‘s’ is stored in ‘s’.

I continuously learn from you guys – keep up the great job you’re doing. It’s awesome that people are so willing to help each other….

Thanks again.

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.