views:

183

answers:

2

I have been working on this source code, but nothing seems to go right. A revised source code would be extremely appreciated, or at least a visual solution to my errors.

Here is the following problem: Write a program that reads three edges for a triangle and determines whether the input is valid. The input is valid if the sum of any two edges is greater than the third edge. Here are the sample runs of this program: Enter three edges 1, 2.5, 1 [Enter] Can edges 1, 2.5, and 1 form a triangle? false

Here is what I have so far for the source code":

#include <iostream>
using namespace std;

bool Valid (int tri_a, int tri_b, int tri_c);
bool triangle; 


int main ()
{
int a;
int b;
int c;
cout << "Enter three edges: ";
double edge1, edge2, edge3;
cin >> edge1 >> edge2 >> edge3;

bool isValid = (edge1 + edge2 > edge3) &&
(edge1 + edge3 > edge2) && (edge3 + edge2 > edge1);
cout << " Enter the 1st value: ";
cin >> a;
cout << " Enter the 2nd value: ";
cin >> b;
cout << " Enter the 3rd value: "; 
cin >> c; 




bool triangle = Valid (a, b, c);

{
if (triangle == true)
cout << "valid" << endl;
else
cout << "invalid" << endl; 
} 

system ("pause");

return 0;

}
+4  A: 

Edit: The answers and comments here are beginning to sound really crazy. As I said, In a triangle, the sum of every two edges must be bigger than the third! 1+1>sqrt(2), 1+sqrt(2)>1, sqrt(2)+1>1 as an example for a 90 degree triangle with two 1 length edges. the logic of isValid is FINE.

Here's a quote from the Wikipedia article:

he sum of the lengths of any two sides of a triangle always exceeds the length of the third side, a principle known as the triangle inequality. Since the vertices of a triangle are assumed to be non-collinear, it is not possible for the sum of the length of two sides be equal to the length of the third side.


Well, you didn't define the Valid method. Also, why are you reading 3 values together and then 3 values one-by-one??

Here's a quick fix, see if it works (haven't tested it):

#include <iostream>
using namespace std;


int main () {
    cout << "Enter three edges: ";
    double edge1, edge2, edge3;
    cin >> edge1 >> edge2 >> edge3;

    bool isValid = (edge1 + edge2 > edge3) &&
    (edge1 + edge3 > edge2) && (edge3 + edge2 > edge1);

    if (isValid)
        cout << "valid" << endl;
    else
        cout << "invalid" << endl;  

    system ("pause"); //not sure if this is right, so I left it here.
    return 0;
}
Amir Rachum
check the logic of the isValid line
Woot4Moo
It's completely fine! In a triangle, the sum of every two edges must be bigger than the third! `1+1>sqrt(2)`, `1+sqrt(2)>1`, `sqrt(2)+1>1` as an example for a 90 degree triangle with two `1` length edges.
Amir Rachum
+1 Other than the (existing) `system` call being questionable, this looks like it should fix all the obvious problems.
Mark B
@Mark: Calls to `system` are not portable, but they are perfectly reasonable so long as the teacher is running the right os and doesn't mind them. The onus is on the OP to know if that is the case.
Brian
@Amir: What are `int a; int b; int c;` for? I see how they were used in the OPs code, but in yours they should probably be scrapped.
Brian
@Brian fixed, thanks.
Amir Rachum
+1  A: 

The solution suggested by Amir Rachum is a good textbook solution. However, it has three practical problems:

  1. It doesn't validate the input (3 positive numerics)

  2. if (edgeX+edgeY>edgeZ) can cause an overflow. A better check would be if (edgeX>edgeZ-edgeY) (after verifying that the numbers are positive).

  3. if input edges are of different scales, It may return false instead of true. for example: 1, 1e300, 1e300.

Here is my suggested solution:

#include <iostream> 
#include <string> 
#include <sstream> 
using namespace std; 

    int main ()
    {
        cout << "Enter three edges: ";

        string edges;
        getline (cin, edges);

        stringstream sstream;
        sstream << edges.c_str(); 

        double edge1= 0., edge2= 0., edge3= 0.; 
        string extra= "";
        sstream >> edge1 >> edge2 >> edge3 >> extra;

        if (edge1>0. && edge2>0. && edge3>0. && extra=="")
        {
            // e.g. 1e300-1 == 1e300
            bool bValid1= (edge3-edge2 == edge3) ? edge1>=edge3 : edge1 > edge3 - edge2;
            bool bValid2= (edge2-edge3 == edge2) ? edge1>=edge2 : edge1 > edge2 - edge3;
            bool bValid3= (edge1-edge2 == edge1) ? edge3>=edge1 : edge3 > edge1 - edge2;

            bool isValid= bValid1 && bValid2 && bValid3;

            if (isValid) 
                cout << "valid" << endl; 
            else 
                cout << "invalid" << endl;
        }
        else
            cout << "invalid input" << endl;

        system ("pause");
        return 0; 
    } 
Lior Kogan