Calculating the area of a triangle

If somebody could give this a look over to help me fix my issue.. The program just finishes after inputting one value and I don't know where I'm going wrong.
Any feedback would be appreciated.

Here are the assignment instructions:

Requirements for getDouble function
Accept three arguments: a string prompt, a double minimum value, and a double maximum value.
Prompt the user to enter a value.
Get a double value from the user.
Display an error message if the value the user entered was out of range.
Continue prompting for input and accepting input until the input is valid. That means most of the above steps must be in a loop.
Once a valid double has been entered, return that double value.
Requirements for isValidTriangle function
Accept three arguments. All three are doubles representing the three lengths of the sides of a triangle.
Return true if the sides represent a valid triangle, and false otherwise.
A triangle is valid if the two smallest sides add up to more than the largest side. It can be a little tricky to figure out how to test this. It is suggested that you first find the longest side. Use a variable to hold the value of the longest side. Start by assuming side 1 is the longest and set the variable to that side's length. Then compare that value to each of the other sides and change the value if needed. When you are done, the "longest" variable will hold the length of the longest side. Then you can just check to see if twice the longest side is less than the sum of all the sides added together. If it is, the triangle is valid. Otherwise, the triangle is invalid.
This function must not display anything.
Requirements for calcArea function
Accept three arguments (all doubles representing the three side lengths).
Use the isValidTriangle function to determine if the triangle is valid.
If the triangle is not valid, then return -1.
If the triangle is valid then return the area of the triangle.
The area of the triangle can be calculated using Heron's formula:
area = (s (s - a) (s - b) (s - c)) ^ 0.5
where ^ 0.5 indicates square root, and
s = half the perimeter of the triangle, and
a, b, and c are the three side lengths
Note: The formula above is presented as math notation - NOT program code. Multiplication is sometimes not explicitly shown. Multiplication must be explicitly shown in program code.
Note: The perimeter is the sum of the sides (a + b + c)
Note: x ^ 0.5 means the square root of x
You can get the square root of x in C++ using the cmath library function: sqrt(x)
This function must not display anything.
Requirements for main function
Display an introductory message: This program calculates the area of a triangle
Use the getDouble function to get the lengths (between 1 and 100) of the three sides from the user. This will require three calls to getDouble.
Use the calcArea function to get the area of the triangle.
If calcArea returns a negative value for the area, then that indicates the triangle is invalid. If the triangle is invalid, then display an error message; otherwise display the area with one digit after the decimal point.


/*
Functions 2 project
Tim Lamoureux
CIS 150
2019-10-09
*/

#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <cmath>
using namespace std;
using std::cin;
using std::cout;

double getDouble (string prompt, double minimum, double maximum);

double isValidTriangle(double length, double width, double height);

double calcArea(double length, double width, double height);

int main() {
cout << "This program calculates the area of a triangle " << endl;
double length, width, height;
bool valid;
valid = false;
double value;
while (!valid) {
cout << "Enter the first side length (1-100): ";
if (cin >> value) {
if (value < 1) {
cout << "Error: Number below minimum value " << 1 << '\n';
} else if (value > 100) {
cout << "Error: Number above maximum value " << 100 << '\n';
} else {
valid = true;
}
} else {
cin.clear();
cin.ignore(500, '\n');
cout << "Error: Invalid number\n";
}
value = length;
}
while (!valid) {
cout << "Enter the second side length (1-100): ";
if (cin >> value) {
if (value < 1) {
cout << "Error: Number below minimum value " << 1 << '\n';
} else if (value > 100) {
cout << "Error: Number above maximum value " << 100 << '\n';
} else {
valid = true;
}
} else {
cin.clear();
cin.ignore(500, '\n');
cout << "Error: Invalid number\n";
}
value = width;
}
while (!valid) {
cout << "Enter the third side length (1-100): ";
if (cin >> value) {
if (value < 1) {
cout << "Error: Number below minimum value " << 1 << '\n';
} else if (value > 100) {
cout << "Error: Number above maximum value " << 100 << '\n';
} else {
valid = true;
}
} else {
cin.clear();
cin.ignore(500, '\n');
cout << "Error: Invalid number\n";
}
value = height;
}
if (length < 0 || width < 0 || height < 0 ||
(length + width <= height) || length + height <= width ||
width + height <= length)
{
cout << "Not a valid triangle";
exit(0);
}
double s = (length + width + height) / 2;
cout << "The area of the triangle is: " << s << endl;
return 0;
}

double getDouble (string prompt, double min, double max) {
bool valid = false;
double value;
while (!valid) {
cout << prompt;
if (cin >> value) {
if (value < 1) {
cout << "Error: Number below minimum value " << 1 << '\n';
} else if (value > 100) {
cout << "Error: Number above maximum value " << 100 << '\n';
} else {
valid = true;
}
} else {
cin.clear();
cin.ignore(500, '\n');
cout << "Error: Invalid number\n";
}
}
return value;
}

double isValidTriangle(double length, double width, double height) {
if (length < 0 || width < 0 || height < 0 ||
(length + width <= height) || (length + height <= width) || (width + height <= length) )
return false;
else
return true;
}

double calcArea(double length, double width, double height) {
if (length < 0 || width < 0 || height < 0 ||
(length + width <= height) || length + height <= width ||
width + height <= length)
{
cout << "Not a valid triangle";
exit(0);
}
double s = (length + width + height) / 2;
return sqrt(s * (s - length) *
(s - width) * (s - height));
}
Use Code Tags, like this:



[code]

//Your Code Here

[/code]



You've put "value = length" and have done that for height and width. I think you meant the opposite "length = value".

The reason your program ends is because the logic is all over the place. You have a single bool that you use. You set it to "true" if the value taken in is within parameters, and the move on to another loop which depends on it to be "false" to even enter the loop. You need to set it back to false after each loop. Below is the correct code to make this work. However, you really should re-code all this, this code is very messy and overly complicated:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// ConsoleApplication5.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <cmath>

using namespace std;

double getDouble(string prompt, double minimum, double maximum);

double isValidTriangle(double length, double width, double height);

double calcArea(double length, double width, double height);

int main() {
	cout << "This program calculates the area of a triangle " << endl;
	double length = 0, width = 0, height = 0;
	bool valid;
	valid = false;
	double value;
	while (!valid) {
		cout << "Enter the first side length (1-100): ";
		if (cin >> value) {
			if (value < 1) {
				cout << "Error: Number below minimum value " << 1 << '\n';
			}
			else if (value > 100) {
				cout << "Error: Number above maximum value " << 100 << '\n';
			}
			else {
				valid = true;
			}
		}
		else {
			cin.clear();
			cin.ignore(500, '\n');
			cout << "Error: Invalid number\n";
		}
		length = value;
	}
	valid = false;
	while (!valid) {
		cout << "Enter the second side length (1-100): ";
		if (cin >> value) 
		{
			if (value < 1) 
			{
				cout << "Error: Number below minimum value " << 1 << '\n';
			}
			else if (value > 100) {
				cout << "Error: Number above maximum value " << 100 << '\n';
			}
			else {
				valid = true;
			}
		}
		else {
			cin.clear();
			cin.ignore(500, '\n');
			cout << "Error: Invalid number\n";
		}
		width = value;
	}
	valid = false;
	while (!valid) {
		cout << "Enter the third side length (1-100): ";
		if (cin >> value) {
			if (value < 1) {
				cout << "Error: Number below minimum value " << 1 << '\n';
			}
			else if (value > 100) {
				cout << "Error: Number above maximum value " << 100 << '\n';
			}
			else {
				valid = true;
			}
		}
		else {
			cin.clear();
			cin.ignore(500, '\n');
			cout << "Error: Invalid number\n";
		}
		height = value;
	}
	if (length < 0 || width < 0 || height < 0 ||
		(length + width <= height) || length + height <= width ||
		width + height <= length)
	{
		cout << "Not a valid triangle";
		exit(0);
	}
	double s = (length + width + height) / 2;
	cout << "The area of the triangle is: " << s << endl;
	return 0;
}

double getDouble(string prompt, double min, double max) {
	bool valid = false;
	double value;
	while (!valid) {
		cout << prompt;
		if (cin >> value) {
			if (value < 1) {
				cout << "Error: Number below minimum value " << 1 << '\n';
			}
			else if (value > 100) {
				cout << "Error: Number above maximum value " << 100 << '\n';
			}
			else {
				valid = true;
			}
		}
		else {
			cin.clear();
			cin.ignore(500, '\n');
			cout << "Error: Invalid number\n";
		}
	}
	return value;
}

double isValidTriangle(double length, double width, double height) {
	if (length < 0 || width < 0 || height < 0 ||
		(length + width <= height) || (length + height <= width) || (width + height <= length))
		return false;
	else
		return true;
}

double calcArea(double length, double width, double height) {
	if (length < 0 || width < 0 || height < 0 ||
		(length + width <= height) || length + height <= width ||
		width + height <= length)
	{
		cout << "Not a valid triangle";
		exit(0);
	}
	double s = (length + width + height) / 2;
	return sqrt(s * (s - length) *
		(s - width) * (s - height));
}
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
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <algorithm> // std:max
#include <cmath> // std::sqrt

bool is_valid_triangle( double a, double b, double c )
{
    // if any side has non-positive length; it is not a triangle
    if( a <= 0 || b <= 0 || c <= 0 ) return false ;

    // triangle inequality:  longest side length is less than the semi-perimeter.
    // https://en.wikipedia.org/wiki/Triangle_inequality
    const double longest_side = std::max( { a, b, c } ) ;
    return (a+b+c) > longest_side*2 ;
}

double get_side( const char* side_name, double min_length, double max_length )
{
    std::cout << "enter the " << side_name << " side length ["
              << min_length << " to " << max_length << "]: ";
    double length ;
    if( std::cin >> length )
    {
        if( length < min_length ) std::cout << "error: length is below minimum value\n" ;
        else if( length > max_length ) std::cout << "error: length is above maximumvalue\n" ;
        else return length ; // valid length, return it
    }

    else // user did not enter a number
    {
        std::cout << "error: non-numeric input\n" ;
        std::cin.clear() ; // clear the failed stated
        std::cin.ignore( 1000, '\n' ) ; // throw the bad input line away
    }

    std::cout << "try again\n" ;
    return get_side( side_name, min_length, max_length ) ; // try again
}

double area_of_triangle( double a, double b, double c ) // invariant: is_valid_triangle(a,b,c)
{
    // heron's formula: https://en.wikipedia.org/wiki/Heron%27s_formula
    const double s = (a+b+c) / 2 ; // semi-perimeter
    return std::sqrt( s * (s-a) * (s-b) * (s-c) ) ;
}

int main()
{
    constexpr double MIN_LENGTH = 1 ;
    constexpr double MAX_LENGTH = 100 ;
    static_assert( MIN_LENGTH > 0 && MIN_LENGTH < MAX_LENGTH, "bad value for min/max length" ) ;

    const double a = get_side( "first", MIN_LENGTH, MAX_LENGTH ) ;
    const double b = get_side( "second", MIN_LENGTH, MAX_LENGTH ) ;
    const double c = get_side( "third", MIN_LENGTH, MAX_LENGTH ) ;
    std::cout << "\nsides are " << a << ", " << b << " and " << c << '\n' ;

    if( is_valid_triangle(a,b,c) )
    {
        const double area = area_of_triangle(a,b,c) ;
        std::cout << "area of triangle is: " << area << '\n' ;
    }

    else std::cout << "these sides do not for a valid triangle\n" ;
}
Here is a one line method to calculate area of triangle from my project.
You can re-implement it if you want to take POINT objects as arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	/**
	* Function to calculate area of triangle formed by (x1, y1), (x2, y2) and (x3, y3)
	*
	* @param x1		x coordinate of a first point
	* @param y1		y coordinate of a first point
	* @param x2		x coordinate of a second point
	* @param y2		y coordinate of a second point
	* @param x3		x coordinate of a third point
	* @param y3		y coordinate of a third point
	*
	* @return		area of a given triangle
	*/
	inline float TriangleArea(int x1, int y1, int x2, int y2, int x3, int y3) noexcept
	{
		return std::abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) * 0.5f);
	}

Topic archived. No new replies allowed.