Calculate perimeter and area of triangle program

Hey there, I am trying to write a program that finds the perimeter and area of a triangle. It will prompt the user to enter the three sides and then it will calculate the outcome. At the end I want it to ask the user if it wants to do the program again. I am also using a do while loop for this program. I made some comments in the program that I am struggling with. Thank you in advance.


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

using namespace std;

int main ()
{
	
float s1, s2, s3;
float perim, area;
float answer;	


// i need to have a header that says Triangle Calculator, and a grpahic of a trianlge made of asterisks


 

do
{


cout << "Enter the first side ";
cin >> s1;

cout << "Enter the second side ";
cin >> s2;

cout << "Enter the third side ";
cin >> s3;
	
perim = s1 + s2 + s3;
// my pseuocode says i need to calculate P right here? not sure what it means.



if ( < 0)           // supposed to print out it is not a triangle if SOMETHING is < 0 but not sure what SOMETHING is.( maybe perim, or area?)
cout << "Not a triangle";


else

area = sqrt(p times (p - s1) times (p - s2) times (p - s3)); // what is P???

cout << "The perimeter is " << perim;
cout << "The area is " << area;					// probably need and end l for these lines
}



// need to prompt user to go again here
while answer ==  //missing something here i think



return 0;
}




First things first, you seem to get confused when 'p' appears. I think whoever wrote your exercise is using it as shorthand for perimeter. So, in your code, you need either 'perim' or 'p' to represent perimeter, rather than alternating between them.

Where you're saying you're not sure how to calculate 'P' you're actually doing it in the line above.

'If something' is < 0 means if any of the sides entered or < 0. It should actually be <= 0. Logically, if a side is zero (or less) it can't be a triangle.

Again, the p in the area calculation means perimeter.

Prompting the user to go again needs to go inside of the loop. The simplest way people do this is something like this:
1
2
3
4
5
6
7
8
9
10
bool running = true;

do
{
   /* Maths stuff */

   // Ask user if they want to go again
   // Get input
   // If input is 'N', set 'running' to false
} while( running );


Hope this helps.
Last edited on
Yeah you are right about the P situation thank you. I just made perim = p instead which worked out fine. The thing about the prompting user to run the program again, I forgot to mention it needs to be something like:

Another (y/n)? n

So it makes the user either hit y or n to either run it again or exit program.
So in this case do I just need to make a cout statement at the end like this

cout << "Another (Y/N)?"

That seems like it would be too simple and I need to do something else besides just that cout statement.
You need that statement, yes.

Then you need something to take the user's input and check to see if it's Y, N or something else (if it's something else you probably want to display an error message).
Hm, Those lines don't show up after the calculations have been made. I want the user to either select y or n so what does that mean I need to set running to? true and false?

Edit: I did figure out the whole p dilemma, it was part of an equation that I needed. So I just need help on this whole asking to run program again deal. Since I want it to prompt after the calculations have been done, don't I need to put the

cout << "Another? (y/n)";
cin >> choice;

at the end of the program?


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

using namespace std;

int main ()
{
	
float s1, s2, s3;
float perim, area, p;
float answer;
int choice;
int y, n;
bool running = true;	

// i need to have a header of a grpahic of a trianlge made of asterisks
cout << "Triangle Calculator";


do
{

cout << "\n\nEnter the first side: ";
cin >> s1;

cout << "Enter the second side: ";
cin >> s2;

cout << "Enter the third side: ";
cin >> s3;
	
perim = s1 + s2 + s3;
p=.5*perim;

if (perim<=0)
cout << "Not a triangle";



else

area=sqrt(p *(p - s1)*(p - s2)*(p - s3)); 

cout << "\nThe perimeter is " << perim << endl;
cout << "The area is " << area << endl;					
}



while (running );


cout << "Another? (y/n)";					// these 2 lines do not show
cin >> choice;													

return 0;
}

Last edited on
Topic archived. No new replies allowed.