C/C++ Calculating Triangle Area Using C++ Programming

Click For Summary
The discussion revolves around a programming project in C++ that requires calculating the area of a triangle based on three user-provided side lengths. The main focus is on using Heron's formula for area calculation and ensuring the validity of the triangle using the triangle inequality theorem. Participants emphasize the importance of checking if the sum of the two smaller sides is greater than the largest side to confirm that a valid triangle can be formed. Several coding issues are addressed, including the correct implementation of Heron's formula, variable declaration, and the need to compute the area only if the radicand (r) is positive. The conversation also touches on debugging, with suggestions to handle error messages and ensure the program does not close unexpectedly. Ultimately, the user learns to implement conditional statements effectively and understands the necessity of fulfilling both the area calculation and triangle validity conditions as part of the assignment requirements.
  • #31
ineedhelpnow said:
i fixed my last post.

Okay, your logic looks good now...I would compile and run...:D
 
Technology news on Phys.org
  • #32
fixed it. ok so do i move the cout statement to the if {} part or do i put it at the bottom of the program?
 
  • #33
ineedhelpnow said:
fixed it. ok so do i move the cout statement to the if {} part or do i put it at the bottom of the program?

You want a different output depending on whether $r$ is positive or not, so it appears to me that they are in the right place.
 
  • #34
do i replace a number with a negative to test else?
 
  • #35
ineedhelpnow said:
do i replace a number with a negative to test else?

You are checking to see if $r$ is positive, and if it is, then you are computing and displaying the area of the triangle. If $r$ is not positive, then the code in the else block is executed, displaying the error message.

Code:
if (0 < r)
{
    //code that executes if r is positive (if the condition 0 < r returns "true").
}
else
{
    //code that executes if r is not positive (if the condition 0 < r returns "false").
}
 
  • #36
can u give me an example on how to test it? i want to make sure that it also prints an error statement.
 
  • #37
ineedhelpnow said:
can u give me an example on how to test it? i want to make sure that it also prints an error statement.

The if statement is already testing $r$ to see if it is positive. We require $r$ to be positive in order to have a valid triangle.

If you want to make certain that the error message is displayed, then let:

Code:
a = 1.0
b = 1.0
c = 3.0

This should trigger an error because $r$ will be negative.
 
  • #38
when i put those numbers in instead, the screen that displays the statement opens for like half a second then closes immediately :(
 
  • #39
ineedhelpnow said:
when i put those numbers in instead, the screen that displays the statement opens for like half a second then closes immediately :(

When the input is valid, does the output window stay open?
 
  • #40
yes but the window should only close if their is something wrong with the program. now it won't even print error.
 
  • #41
ineedhelpnow said:
yes but the window should only close if their is something wrong with the program. now it won't even print error.

The only thing I can suggest is to make sure you are using [m]cout[/m] correctly by reading the documentation provided...I am not familiar with this method of output.
 
  • #42
is it possible that the cin statement is causing a problem?

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));
areaofTriangle=sqrt(r);

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}

- - - Updated - - -

so i definitely don't need to use && commands for the if statement?
 
  • #43
Don't calculate the square root until you're sure that $r$ is positive.
 
  • #44
ineedhelpnow said:
is it possible that the cin statement is causing a problem?

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));
areaofTriangle=sqrt(r);

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}

- - - Updated - - -

so i definitely don't need to use && commands for the if statement?

The error is that you are computing the area of the triangle before testing the value of $r$. :D

Try:

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}
 
  • #45
thats why i have if (r>0)

How do i test the value of r?
 
  • #46
ineedhelpnow said:
thats why i have if (r>0)

How do i test the value of r?

The if statement is testing $r$ to see if it is positive...
 
  • #47
MarkFL said:
The error is that you are computing the area of the triangle before testing the value of $r$. :D

Try:

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}

what did you do different? (mark you HAVE to get rid of the time gap between posts)

- - - Updated - - -

oh i see. well its still closing the window immediately
 
  • #48
ineedhelpnow said:
what did you do different? (mark you HAVE to get rid of the time gap between posts)

I removed the statement computing the area of the triangle before the if-else construct. We only want to compute the area once we are certain that the radicand $r$ is positive.

Flood control is a standard feature on all forums that I have ever been a part of. It is there to prevent users from flooding a thread or forum with posts made in rapid succession. :D
 
  • #49
i removed that statement and its STILL not working (ps i appreciate all your help)

(flood control is driving me nuts. just let the flood be (Dull))
 
  • #50
ineedhelpnow said:
i removed that statement and its STILL not working (ps i appreciate all your help)

Okay, did you check the provided documentation on the output statement [m]cout[/m] to see if your syntax is correct?
 
  • #51
i didnt see a document but I am pretty sure I am doing the cout statements correctly. buuuuuuuut anyways I GOT IT I GOT IT I GOT IT I GOT IT! i needed a cin statement for the else command. thank u!
 
  • #52
ineedhelpnow said:
i didnt see a document but I am pretty sure I am doing the cout statements correctly. buuuuuuuut anyways I GOT IT I GOT IT I GOT IT I GOT IT! i needed a cin statement for the else command. thank u!

That was actually going to be my next suggestion...:D

Glad you got it working correctly! (Yes)
 
  • #53
MarkFL said:
That was actually going to be my next suggestion...:D
:rolleyes: sure it was

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
       cin >> areaofTriangle;
}

return 0;
}
 
  • #54
i actually didnt meet one of the conditions. that two of the sides has to be greater than one of them. or something like that...
 
  • #55
ineedhelpnow said:
i actually didnt meet one of the conditions. that two of the sides has to be greater than one of them. or something like that...

The condition that [m]r[/m] must be positive is equivalent to satisfying the triangle inequality. :D
 
  • #56
(Shake) he failed me on the assignment because i missed that second condition. he's giving a second chance though. also in my program, i stored the variables a b and c which was wrong.
 
  • #57
ineedhelpnow said:
(Shake) he failed me on the assignment because i missed that second condition. he's giving a second chance though. also in my program, i stored the variables a b and c which was wrong.

If it were me, I would present to my professor an algebraic argument demonstrating that the positive value of [m]r[/m] is in fact equivalent to the triangle inequality.
 
  • #58
yeah i could present him with a present lol i don't think he would care. he told me that i had to satisfy both conditions. i don't understand why because the program would still run properly once i get rid of what i stored for a,b, and c but he said i have to put that second condition.
 
  • #59
ineedhelpnow said:
yeah i could present him with a present lol i don't think he would care. he told me that i had to satisfy both conditions. i don't understand why because the program would still run properly once i get rid of what i stored for a,b, and c but he said i have to put that second condition.

I would still make such an argument, but this is up to you. The way you have it coded now is much more efficient than having to determine which is the largest of the 3 values, and then make certain that the sum of the other two sides is greater than it. But, if in the end your professor is adamant and unmoved by a well-reasoned argument, you must do what you must do.
 
  • #60
he wants us to fulfill the second condition because he wants us to be able to code it properly. to have us use all the different types of commands and functions.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
3K
Replies
2
Views
3K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K