Something wrong with the program. The calculations arent correct and IDK what is happening with the setw()

Heres the instructions:

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;

int main()
{
double area, perimeter, s;
double letterA, letterB, letterC;


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;


system("pause");

return 0;
}
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.

Last edited on
so i should say this instead:


double letterA, letterB, letterC;
double area, perimeter, s;


and how do i initialize letterA, letterB, and letterC
You initialized them with the cin command by setting them from the console. Don't use them until they've been set.

this means use cin and THEN do your equations

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
//Herons Formula
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double area, perimeter, s;
	double letterA, letterB, 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;

	//This stuff is moved to BELOW the cin//////////////////
	perimeter = letterA + letterB + letterC;              //
	s = perimeter/2;                                      //
	                                                      //
	area = sqrt(s*(s-letterA)*(s-letterB)*(s-letterC));   //
	////////////////////////////////////////////////////////

	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;

	// DO THE CALCULATION AGAIN/////////////////////////////
	perimeter = letterA + letterB + letterC;              //
	s = perimeter/2;                                      //
	                                                      //
	area = sqrt(s*(s-letterA)*(s-letterB)*(s-letterC));   //
	////////////////////////////////////////////////////////

	cout << "Perimeter= " << setw(7) << left << perimeter << endl; //
	cout << "Area= " << setw(7) << left << area << endl;


	system("pause");

	return 0;
}


ALSO, use code tags.
ok what are code tags. and im just having trouble getting the proper setw()

I changed the code like you said, but the outputs are supposed to give the following numerical values:

Sample Run 1:

The sides of the triangle are:
A = 7.5000; B = 4.5000; C = 6.0000

Perimeter = 18.0000
Area = 13.5000



Sample Run 2:

The sides of the triangle are:
A = 4.1000; B = 4.1000; C = 5.3000

Perimeter = 13.5000
Area = 8.2905

How can i fix this
Last edited on
Code tags are
[code]
and
[/code]


You place the
[code]
before the start of your code and the
[/code]
after the end of your code.

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.

Sorry can't help with setw.
Last edited on
Topic archived. No new replies allowed.