Help me fix my code ?

I'm desperate since I've literally tried everything else with no luck :l

Here are the instructions:
Write a program that computes the area of a triangle using Heron’s formula.
Prompt the user for the lengths of the three sides, say a, b, and c.

Heron’s formula is: Area = sqrt(s(s − a)(s − b)(s − c)),
where s = (a+b+c) / 2 , the semi-perimeter.

1. Create code to input the three values, a, b, and c which are the sides of the triangle.
2. Create two variables, one for area and one for semi-perimeter.
3. Compute the semi-perimeter and the quantity under the radical.
4. If the area squared is negative or zero you do not have a triangle. Output the appropriate
message.
5. If the area squared is positive you have a triangle.
6. Test your program with these values for (a, b, c):
(5, 7, 9), (6.2, 3.7, 4.5), (15, 11, 8.1), (8, 15, 17), (6, 15, 8)
and another triple of your choice.

• The output should look like the one of following:

The triangle with sides: #1, #2, #3 is not a triangle
The triangle with sides: #1, #2, #3 has semi-perimeter: #p and area: #a

Note: The program should avoid trying to compute the square root of a negative number.
(Your program has to check negative square roots)


Basically, it's not supposed to compute any user input that's <= 0. My prof doesn't want us to check user input immediately, but rather check for negative square roots and I'm not sure how to go about doing that.

Here's what I've been working on:

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
#include <iostream>		// Header file needed for I/O
#include <cmath>
using namespace std; 		// Line used for simplicity


int main()
{	
	// Define variables
	double a, b, c; 				
	double s, area;
	
	// Get value of the 3 sides
	cout << "Input the first side of the triangle: ";
	cin >> a;
	cout << "Input the second side of the triangle: ";
	cin >> b;
	cout << "Input the third side of the triangle: ";
	cin >> c;
	cout << endl;

	// Input formula
	s = (a+b+c) / 2;
	area = sqrt(s * ((s-a) * (s-b) * (s-c))); 
		
	// Set conditions
	if  ((area * area) > 0) {
	cout << "The triangle with sides " << a << ", " << b << ", " << c << " has semi-perimeter: "<< s <<" and area: " << area << endl;
	} 

	else {
	cout << "The triangle with sides " << a << ", " << b << ", " << c << " is not a triangle."<< endl;
	}
	
	return 0;
	
}
	


** If I input 5, 7, & 9 then it outputs as I want. If I put 5, -7, 9, then it still gives me the S & area even when it's not supposed to.
Last edited on
Well if the area of a triangle is negative, area*area would be positive.
> If I put 5, -7, 9, then it still gives me the S & area even when it's not supposed to.
> My prof doesn't want us to check user input immediately, but rather check for negative
> square roots and I'm not sure how to go about doing that.

According to your professor, there is a valid triangle with sides of lengths 5, -7, 9
semi perimeter == (a+b+c) / 2 == 3.5
quantity under the radical == s * (s-a) * (s-b) * (s-c) is positive (303.188)


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
#include <iostream>
#include <cmath>

int main()
{
    // Create code to input the three values, a, b, and c which are the sides of the triangle.	double a, b, c;
    std::cout << "enter the absolute values of the lengths of the three sides of the triangle: " ;
    double a, b, c ;
    std::cin >> a >> b >> c ;

    // Compute the semi-perimeter and the quantity under the radical.
    // note: Heron's formula is numerically unstable for triangles with a very small angle when using floating point arithmetic
    // https://en.wikipedia.org/wiki/Heron%27s_formula#Numerical_stability
    const double s = (a+b+c) / 2 ;
    const double qur = s * (s-a) * (s-b) * (s-c) ;

    // If the area squared quantity under the radical is negative or zero
    // you do not have a triangle. Output the appropriate message.
    if( qur <= 0 )
    {
        std::cout << "\nsides " << a << ", " << b << " and " << c
                  << " do not form a triangle\n" ;
    }

    else // If the area squared quantity under the radical is positive you have a triangle
    {
        const double area = std::sqrt(qur) ;
        std::cout << "\nThe triangle with sides " << a << ", " << b << " and " << c
                  << " has semi-perimeter: " << s << " and area: " << area << '\n' ;

        if( a<=0|| b<=0 || c<=0 ) std::cout << "(note: this is not a valid triangle)\n" ;
    }
}

http://coliru.stacked-crooked.com/a/f33ef367d399a0ba
Topic archived. No new replies allowed.