Functions, using sqaureroot

My class assignment is to "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."

I also have two implement the following two functions
//Returns true if the sum of any two sides is greater than the third.

//Returns the area of the triangle
bool isValid(double side1, double side2, double side3) and

So far this is what I have but it is displaying the wrong result since the result is a mix of numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cmath>
using namespace std;

	double area(double side1, double side2, double side3)
	{
	 
	  double s = (side1 + side2 + side3) / 2.0;
	  double area = sqrt(s*(s - side1)*(s - side2)*(s - side3));
	  return area;
	  
	
	}

	 //Returns true if the sum of any two sides is greater than the third side.
	bool isValid(double side1, double side2, double side3)
	{
		if ((side1 + side2) > side3)
		{	
			return true;
			
			
			
			}
		
		else if ((side1 + side2) < side3)
			return false;
	}




	



int main()
{

	double side1, side2, side3;

	cout << " Please enter three sides of a triangle: " << " "; 
	cin >> side1 >> side2 >> side3;
	
	if ( isValid(side1, side2, side3) )
	cout << " The area for the valid triangle is" << " " << area(side1,side2,side3) << endl;
else
    // here output an error message for invalid input 
	cout <<"The triangle is invalid." << endl;
	


	system("pause");
		return 0;
}
Last edited on
Please edit your post and use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

it is displaying the wrong result

What results were you expecting? Be more specific.
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.
Last edited on
Chervil, Thank you very much. I am rather new to c++ and it is my first language I have ever attempted so I am having a bit of trouble catching on. In this problem I see what I was doing wrong thanks to your advice. The bool function is just used to determine if you have to use the area function. It is working properly now so thank you very much.
Also for the function to check the three separate conditions you are talking about since I have the first one ((side1+side2) < side 3 ) could I just use an else statement in the isValid function that returns false?
could I just use an else statement in the isValid function that returns false?
I don't think that would be sufficient. The way I see it, there are four possible situations:
1. side 3 is too big
2. side 2 is too big
3. side 1 is too big
4. Everything is ok


There are lots of ways to code the isValid() function, including trying to do it all in just one (very long) line of code.

I tried it, and I had a line like this:
 
    if ((side1 + side2) < side3) return false;

as I mentioned, that covers the case such as 3, 4, 8

I then added two almost identical lines to cover 3, 8, 4 and 8, 3, 4 (just examples)

and finally, if none of those statement catch a problem, the last line of the function was simply
 
    return true;


(in my version I didn't use else at all).
Alright, once again Thank you very much for your feedback it has helped me a lot.
ohh okay I see what you are saying now. sorry I was not following you. so since I have

((side1 + side2) < side 3) return false;

I will also need a statement such as

((side2 + side1) < side 3) return false ;

right?
Last edited on
In a general sense, yes that's what I meant. Though the two code samples you gave in the last post both do exactly the same thing, which is not what I meant :)
Topic archived. No new replies allowed.