Modify Triangle Program #2 - Enter Valid Sides for Area

In summary: Error: The sides entered do not represent a valid triangle"; } } A = sqrt(r); cout << "The area of the triangle is: " << A; cout << "Continue? (y/n)"; cin >> cont; if (tolower(cont) == "n") {
  • #1
ineedhelpnow
651
0
ok so in this program, if three valid sides are given it displays the area of a triangle otherwise it will ask the user to re enter sides.

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

int main(){
double a,b,c=0;
double s,A=0;

cin>>a>>b>>c;
cout<<"Enter three sides: "<<a<<" "<<b<<" "<<c<<endl;
while(!(a<=0||b<=0||c<=0||((a+b)<=0)||((a+c)<=0)||((b+c)<=0))){
  
    if((a>0)&&(b>0)&&(c>0)&&((a+b)>c)&&((a+c)>b)&&((b+c)>a)){
       s=(a+b+c)/2;
    A=sqrt(s*(s-a)*(s-b)*(s-c));

       cout<<A;
    }
    else{
        cout<<"Enter three sides: "<<a<<" "<<b<<" "<<c<<endl;
    }
   
    }
   return 0;
}

This code needs to be modified so that a user is required to keep entering the three side until they decide to stop. If the three sides entered make an invalid triangle, the user is required to re-enter the values until valid triangle is formed. Then the area is displayed. In other words, the output needs to look like this.

Enter three sides of a triangle: 0 1 2 Error!

Re-enter three sides of a triangle: -1 1 2 Error!

Re-enter three sides of a triangle: 3 4 5 => 6

Continue (y/n)? y

Enter three sides of a triangle: 1 1 2 Error!

Re-enter three sides of a triangle: 6 8 10 => 24

Continue (y/n)? n

Done!(P.S. "Continue (y/n)?" is only displayed after a valid triangle is formed. Otherwise, the user needs to re-enter sides until it's valid.)
i need help with this. any hints?
 
Technology news on Phys.org
  • #2
Here is a basic implementation you can dress up...

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main() {
   double a = 0, b = 0, c = 0, A = 0, s = 0, r = 0;
   int valid = 0, quit = 0;
   char cont = "";

   while (!quit)
   {
      valid = 0;
      while (!valid)
      {
          cout << "Enter the three side lengths of a triangle:";
          cin >> a >> b >> c;
          s = (a + b + c)/2;
          r = s*(s - a)*(s - b)*(s - c));
          if ((a > 0) && (b > 0) && (c > 0) && (r > 0))
          {
              quit = 1;
          }
          else
          {
              cout << "Error: The sides entered do not represent a valid triangle";
          }
      }

      A = sqrt(r);
      cout <<  "The area of the triangle is: " << A;
      cout << "Continue? (y/n)";
      cin >> cont;
      if (tolower(cont) == "n")
      {
         quit = 1;
      }
   }
}

Note that when asked to continue, only entering an "N" or a "n" will terminate the program. You may want to add some code to ensure only a "Y", "y", "N" or "n" is accepted using a while loop.
 
  • #3
MarkFL said:
Here is a basic implementation you can dress up...

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main() {
   double a = 0, b = 0, c = 0, A = 0, s = 0, r = 0;
   int valid = 0, quit = 0;
   char cont = "";

   while (!quit)
   {
      valid = 0;
      while (!valid)
      {
          cout << "Enter the three side lengths of a triangle:";
          cin >> a >> b >> c;
          s = (a + b + c)/2;
          r = s*(s - a)*(s - b)*(s - c));
          if ((a > 0) && (b > 0) && (c > 0) && (r > 0))
          {
              quit = 1;
          }
          else
          {
              cout << "Error: The sides entered do not represent a valid triangle";
          }
      }

      A = sqrt(r);
      cout <<  "The area of the triangle is: " << A;
      cout << "Continue? (y/n)";
      cin >> cont;
      if (tolower(cont) == "n")
      {
         quit = 1;
      }
   }
}

Note that when asked to continue, only entering an "N" or a "n" will terminate the program. You may want to add some code to ensure only a "Y", "y", "N" or "n" is accepted using a while loop.

:eek: how do i do that? i remember there was like a break function or something but I am not sure...
 
  • #4
You could do something like:

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main() {
   double a = 0, b = 0, c = 0, A = 0, s = 0, r = 0;
   int valid = 0, quit = 0;
   char cont = "";

   while (!quit)
   {
      valid = 0;
      while (!valid)
      {
          cout << "Enter the three side lengths of a triangle:";
          cin >> a >> b >> c;
          s = (a + b + c)/2;
          r = s*(s - a)*(s - b)*(s - c));
          if ((a > 0) && (b > 0) && (c > 0) && (r > 0))
          {
              quit = 1;
          }
          else
          {
              cout << "Error: The sides entered do not represent a valid triangle";
          }
      }

      A = sqrt(r);
      cout <<  "The area of the triangle is: " << A;
      cont = "";
      while ((cont != "y") || (cont != "n"))
      {
         cout << "Continue? (y/n)";
         cin >> cont;
         cont = tolower(cont);
         if ((cont != "y") || (cont != "n"))
         {
            cout << "Please enter either y or n";
         }
      }
      if (cont == "n")
      {
         quit = 1;
      }
   }
}
 
  • #5
Mark is a better geometer than I. Notice that his code says a triangle is valid if the three lengths a, b and c are positive and r is positive. This replaces your original condition that of a, b and c, each is less than the sum of the other two. Since this is a math forum, you should try and verify this alternate condition for a valid triangle. (I wasn't aware of this until now.)
 
  • #6
johng said:
Mark is a better geometer than I. Notice that his code says a triangle is valid if the three lengths a, b and c are positive and r is positive. This replaces your original condition that of a, b and c, each is less than the sum of the other two. Since this is a math forum, you should try and verify this alternate condition for a valid triangle. (I wasn't aware of this until now.)

It's funny you should mention this...

I actually did demonstrate this in another thread, but Pippy says her prof. won't allow using this much more efficient condition, but I refuse not to use it...:D
 
  • #7
yes the reason why is because he gave us the variables a and b and c and s and A to use ONLY. i asked him and he told me not to add anything extra to it (he wants to make our life harder (Dull)). so all the conditions i listed are correct he said. because like in the instructions he said make sure for the if loop that the sum of two sides are greater than one side. he actually wants to see us do that instead of taking the short and cut and just showing that if r is valid then the triangle is. so yeah. unfortunately we have to verify those conditions he gave us. not for the sake of math but he wants us to become familiar with using ALL the programming functions multiple times so we get the hang of it. between mhb and me i love marks answer. (Giggle). my instructor docks points if we don't follow his instructions.
 
  • #8
(Yawn) I am stuck again. does anyone have anymore hints? i need help with the do while loop and also i don't know where or when to print what.
 
  • #9
Okay, let's focus first on getting valid input from the user for the sides of the triangle.

You are going to want a loop that gets iterated at least once, and then if the input is not valid, we want it to be interated again, and until we get valid input. So, a do while loop is what we want, because this will be iterated, and then if the condition given at the end is true, the loop will be iterated again. Within the loop, we want to get the input.

So, can you construct a do while loop, with the input prompt in the body, and then the appropriate condition requiring the loop to be reiterated?
 
  • #10
he showed us what the output should look like. I am having trouble getting which part or what should be print out from the do while loop.
 
  • #11
This is what I have in mind:

Code:
do
{
   cout << "Enter the three side lengths of a triangle:";
   cin >> a >> b >> c;
   if (!((a > 0) && (b > 0) && (c > 0) && ((a + b) > c) && ((a + c) > b) && ((b + c) > a)))
   {
      cout << "Error!";
   }
} while (!((a > 0) && (b > 0) && (c > 0) && ((a + b) > c) && ((a + c) > b) && ((b + c) > a)))
 
  • #12
where does the do you want to continue (y/n) part come in though?
 
  • #13
ineedhelpnow said:
where does the do you want to continue (y/n) part come in though?

That will come after the area of the triangle is computed and displayed, and at that point we will want to wrap the main body of the program within a do while loop as well, so that it gets iterated over and over, until the user enters "N" or "n" to the continue prompt.

I just wanted to break the problem up into smaller parts...that's how I code, and it makes things more manageable mentally.
 
  • #14
Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

   double a = 0, b = 0, c = 0, s = 0, A = 0;
   char answer = 'y';

   do
   {      do
      {
        cout << "Enter the three side: ";
            cin >> a >> b >> c;
            cout<<a<<" "<<b<<" "<<c<<endl;
     
         if (!((a > 0) && (b > 0) && (c > 0) && ((a + b) > c) && ((a + c) > b) && ((b + c) > a)))
         {
            cout << "Error!"<<endl<<"Re-";
         }
      } while (!((a > 0) && (b > 0) && (c > 0) && ((a + b) > c) && ((a + c) > b) && ((b + c) > a)));

      s = (a + b + c)/2;
      A = sqrt(s*(s - a)*(s - b)*(s - c));
      cout << A;

     
      
         answer = ' ';
         cout <<endl<< "Continue? (y/n)";
         cin >> answer;
         cout<<" "<<answer<<endl;
      
   } while (answer == 'y');
   
   if(answer=='n')
   cout<<"Done!";

   return 0;
}

ok so this is the code that a buddy of mine helped me with. this is the output when i test the sides 5 and 8 and 7
Enter the three side: 5 8 7
17.3205
Continue? (y/n) y
Enter the three side: 5 8 7
17.3205
Continue? (y/n)

whats happening is that because the while condition says as long as the user types y it repeats until it comes back to continue? (y/n) here is what i want to happen though
Enter the three side: 5 8 7
17.3205
Continue? (y/n) y
Enter the three side:

is there a way to do this? also when i test it with something like -5 8 7 i get:
Enter the three side: -5 8 7
Error!
Re-Enter the three side: -5 8 7
Error!
Re-Enter the three side: -5 8 7
Error!
Re-Enter the three side: -5 8 7
Error!
Re-Enter the three side: -5 8 7
Error!
Re-Enter the three side: -5 8 7
Error!
Re-Enter the three side: -5 8 7
Error!
Re-Enter the three side: -5 8 7
Error!
...
...
...
...
and it goes on and on and on because the loop

but all i want to happen is:
Enter the three side: -5 8 7
Error!
Re-Enter the three side:

how do i do this? please help because the assignment is due in two hours.
 
  • #15
I found a code snippet >>>here<<< that may be helpful:

Code:
// Description: Illustrate the use of cin to get input
// and how to recover from errors.

#include <iostream>
using namespace std;

int main()
{
  int input_var = 0;
  // Enter the do while loop and stay there until either
  // a non-numeric is entered, or -1 is entered.  Note that
  // cin will accept any integer, 4, 40, 400, etc.
  do {
    cout << "Enter a number (-1 = quit): ";
    // The following line accepts input from the keyboard into
    // variable input_var.
    // cin returns false if an input operation fails, that is, if
    // something other than an int (the type of input_var) is entered.
    if (!(cin >> input_var)) {
      cout << "Please enter numbers only." << endl;
      cin.clear();
      cin.ignore(10000,'\n');
    }
    if (input_var != -1) {
      cout << "You entered " << input_var << endl;
    }
  }
  while (input_var != -1);
  cout << "All done." << endl;

  return 0;
}
 
  • #16
Pippy is worn out so whatever she got, she going to upload (Yawn)
thank you mark
 

FAQ: Modify Triangle Program #2 - Enter Valid Sides for Area

What is the purpose of the "Modify Triangle Program #2"?

The purpose of the "Modify Triangle Program #2" is to create a program that calculates the area of a triangle using valid side lengths entered by the user.

How does the program ensure that the entered side lengths are valid?

The program uses conditional statements to check if the entered side lengths satisfy the criteria for a valid triangle. This includes ensuring that each side length is greater than 0 and that the sum of any two sides is greater than the third side.

Can the program handle decimal or negative values for the side lengths?

No, the program only accepts positive integer values for the side lengths. If the user enters a decimal or negative value, the program will prompt them to enter a valid side length.

What happens if the user enters non-numeric characters for the side lengths?

If the user enters non-numeric characters, the program will display an error message and prompt the user to enter valid side lengths.

Can the program calculate the area of any type of triangle?

Yes, the program can calculate the area of any type of triangle as long as the entered side lengths satisfy the criteria for a valid triangle. This includes equilateral, isosceles, and scalene triangles.

Similar threads

Replies
12
Views
3K
Replies
1
Views
4K
Replies
5
Views
2K
Replies
60
Views
8K
Back
Top