Your program should use the C++ standard Math Library function to
compute the square root in the formula below. Be sure to use the relevant
include directives. Assume that all input values are positive real numbers
that satisfy the triangle inequality.
Relevant Formulas:
Let A, B and C denote the sides of the triangle:
Perimeter = A + B + C
Area = sqrt(S(S − A)(S − B)(S − C)), where S = Perimeter/2
Typical program interactions should appear as below:
Sample Run 1:
Enter the lengths of the sides of a triangle> 7.5 4.5 6
The sides of the triangle are:
A = 7.5000; B = 4.5000; C = 6.0000
Perimeter = 18.0000
Area = 13.5000
Sample Run 2:
Enter the lengths of the sides of a triangle> 4.1 4.1 5.3
The sides of the triangle are:
A = 4.1000; B = 4.1000; C = 5.3000
Perimeter = 13.5000
Area = 8.2905
Here's my program. I need help fixing the letterA, letterB, and letterC doubles. The program wants me to define it, but it is supposed to be user-defined. Please tell me anything else that is not making the program run as it should:
//Herons Formula
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
perimeter = letterA + letterB + letterC;
s = perimeter/2;
area = sqrt(s*(s-letterA)*(s-letterB)*(s-letterC));
cout << "Sample Run 1: " << endl;
cout << endl;
cout << "Enter the lengths of the sides of a triangle> ";
cin >> letterA >> letterB >> letterC;
cout << endl;
cout << "The sides of the triangle are: " << endl;
cout << " A = " << setw(6) << left << letterA;
cout << " B = " << setw(6) << left << letterB;
cout << " C = " << setw(6) << left << letterC << endl;
cout << endl;
cout << "Perimeter= " << setw(7) << left << perimeter << endl; //
cout << "Area= " << setw(7) << left << area << endl;
cout << endl;
cout << endl;
//This space is reserved for Sample Run 2
cout << "Sample Run 2: " << endl;
cout << endl;
cout << "Enter the lengths of the sides of a triangle> ";
cin >> letterA >> letterB >> letterC;
cout << endl;
cout << "The sides of the triangle are: " << endl;
cout << " A = " << setw(6) << left << letterA;
cout << " B = " << setw(6) << left << letterB;
cout << " C = " << setw(6) << left << letterC << endl;
cout << endl;
cout << "Perimeter= " << setw(7) << left << perimeter << endl; //
cout << "Area= " << setw(7) << left << area << endl;
You didn't do as Stewbond told you in the other thread.
1 2 3 4
//This block must go after all definitions and cin but before the cout for area.
perimeter=A+B+C; //I dropped the quotes here
s= perimeter/2;
area= sqrt(s*(s-A)*(s-B)*(s-C)); // I dropped the quotes here
These must go after cin.
You declare letterA, letterB, and letterC but they are not initialised and so contain garbage.
perimeter then also contains garbage, s contains garbage and area contains garbage.
After you cin, then letterA, letterB and letterC contain sensible values input by the user but perimeter, s and area still contain garbage because you calculated them before letterA, letterB and letterC had the values input from the user.
Another way to do this is to select your lines of code so they are highlighted. If you look at the right of a post when you are creating it or editing it you will see 12 buttons under the heading Format:
If you press the
<>
button at the top left of the buttons it will put code tags at the start and end of the highlighted text for you.