Modify Triangle Program #2 - Enter Valid Sides for Area

  • Context: MHB 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Program Triangle
Click For Summary

Discussion Overview

The discussion revolves around modifying a C++ program that calculates the area of a triangle based on user-inputted side lengths. The focus is on ensuring that the program continuously prompts the user for valid triangle sides until they choose to stop, while also handling invalid inputs appropriately.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes the current program structure and its requirement to display the area of a triangle if valid sides are entered, otherwise prompting for re-entry.
  • Another participant provides a modified code snippet that includes a loop for continuous input until valid triangle sides are provided, suggesting the use of a character input to continue or stop the program.
  • Some participants discuss the conditions for a valid triangle, with one noting the use of an alternate condition based on the semi-perimeter, while another insists on adhering to the instructor's specified conditions regarding side lengths.
  • A participant expresses confusion about implementing a do-while loop for input validation and seeks clarification on where to include the continuation prompt.
  • Another participant suggests a structure for the do-while loop to ensure valid input is obtained before proceeding to calculate the area.

Areas of Agreement / Disagreement

There is no consensus on the best approach to implement the program modifications. Participants present differing views on the conditions for triangle validity and how to structure the program flow, particularly regarding the use of specific programming constructs.

Contextual Notes

Participants mention specific requirements from an instructor regarding the use of certain variables and conditions, which may limit the flexibility of proposed solutions. There are also discussions about the efficiency of different methods for validating triangle sides.

Who May Find This Useful

Students learning C++ programming, particularly those focused on input validation and control structures, as well as those interested in geometric calculations and triangle properties.

ineedhelpnow
Messages
649
Reaction score
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
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.
 
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.

:o how do i do that? i remember there was like a break function or something but I am not sure...
 
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;
      }
   }
}
 
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.)
 
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
 
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.
 
(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.
 
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
 

Similar threads

Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 60 ·
3
Replies
60
Views
9K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K