There are a few comments to make. First function area() looks very good. It seems to implement Heron's formula for the area of a triangle correctly. As a matter of good practice I would put 2.0 (type double) rather than 2 (type integer) but nevertheless the code does work correctly.
The logic in main() doesn't follow the given assignment, "write a program that reads three sides for a triangle and computes the area if the input is valid, otherwise displays is invalid to the screen."
It should go something like this, after getting the values for the three sides from the user:
1 2 3 4
|
if ( isValid(side1, side2, side3) )
// here call the function area() and output the result
else
// here output an error message for invalid input
|
Currently your code does not properly call any of the functions - I showed above how to call the isValid() function, area() is similar, but of course it returns the calculated result rather than just true or false.
Function
isValid()
needs some attention. There should not be any
cout
message inside the function - leave that to the part of the program where the function is called. It should simply return either
true
or
false
.
Also, the function needs to check three separate conditions. If
((side1 + side2) < side3)
then the triangle is invalid (example 3, 4, 8). You also need to add almost the same code to check for say (3, 8, 4) and (8, 3, 4) which are also invalid.