I created this code to validate the sides of a triangle. Could someone give it a look over and tell me what I need to do. I am getting this single error message
#include <iostream>
usingnamespace std;
int isValid (int, int, int);
int main ()
{
int a;
int b;
int c;
// Prompt the user to enter 3 values
cout << " Enter the 1st value: ";
cin >> a;
cout << " Enter the 2nd value: ";
cin >> b;
cout << " Enter the 3rd value: ";
cin >> c;
system ("pause");
return 0;
} //end main
// The Function Main is executed, then calls the isValid function:
int isValid (int tri_a, int tri_b, int tri_c)
// Determine if the user inputs are valid.
{
if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
cout << "Valid" << endl;
else
cout << "Invalid" << endl;
}
You defined isValid to return an integer, but inside the function you neglect to return anything ever. Also, after main is done executing, the program ends. You have to call the function inside main.
Think about what L B and Jdc1197 said. Also I'm not sure if you're validation if correct, I always use Pythagorean Theorem when validating sides of triangle. Which states that c2 = a2 + b2
[edit]
If that validation of OP does work, please someone put up some link explaining how that validates the sides of a triangle.. thanks.
[edit]
Oh, I just remembered.. Pythagorean theorem only applies to right triangles..
.....as well as what has been said above the following: if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
I think should be changed to: if ((tri_a + tri_b > tri_c) || (tri_a + tri_c > tri_b) || (tri_b + tri_c > tri_a))
No, the use of && is correct. The triangle inequality states that, given a triangle, "the sum of the lengths of any two sides must be greater than the length of the remaining side." (Wikipedia)
Let's say we have a line of length 5 units. Now, attach two lines of length 1 unit each to either end of the 5 unit line. No matter what you do with the two smaller lines, you cannot attach them to make a triangle. If you use || this will be treated as a valid triangle. If you use && this will be treated as an invalid triangle, as it rightfully should.
If the Apple machine has xcode installed, then you can use g++ in a terminal to compile. Or if you really wanted you could use xcode, but I find it rather confusing.
#include <iostream>
usingnamespace std;
void isValid (int, int, int);
int main ()
{
int a;
int b;
int c;
// Prompt the user to enter 3 values
cout << " Enter the 1st value: ";
cin >> a;
cout << " Enter the 2nd value: ";
cin >> b;
cout << " Enter the 3rd value: ";
cin >> c;
system ("pause");
return 0;
} //end main
// The Function Main is executed, then calls the isValid function:
void isValid (int tri_a, int tri_b, int tri_c)
// Determine if the user inputs are valid.
{
if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
cout << "Valid" << endl;
else
cout << "Invalid" << endl;
}
But how and where would I call my isValid function? Could someone write it in for me? thanks
@ shacktar: I would have thought that the definition should be that the two lesser sides when added together should be greater than the remaining greatest side.
while(1)
{ cin>>s1>>s2>>s3;
if(s1==0 )break;
if(s3>(s1+s2)||s1>(s2+s3)||s2>(s3+s1))
cout<<"your side lengths will NOT make a triangle";
else cout<<"your side lengths will make a triangle";
}
The output is:
/*
this determines if 3 side lengths
will make a triangle
enter the 3 side lengths separated by spaces (0 to quit)
14.7 8.1 6.8
your side lengths will make a triangle
*/
....the or logic appears to work?
if(s3>(s1+s2)||s1>(s2+s3)||s2>(s3+s1))
cout<<"your side lengths will NOT make a triangle";
else cout<<"your side lengths will make a triangle";
is the inverse of the triangle inequality (i.e. given a triangle, it is an invalid triangle if any of its sides is greater than the sum of the remaining two).
Given that change, the logic would be "given a triangle, it is valid if the sum of the lengths of at least one pair of its sides is greater than the length of the remaining side." This logic is different than that of the triangle inequality.
But yes, it's sufficient to check that the sum of the two smaller lengths is greater than the remaining greatest length.
shakctar wrote: is the inverse of the triangle inequality (i.e. given a triangle, it is an invalid triangle if any of its sides is greater than the sum of the remaining two).
Yes....i was just too dumb to see it.
Forgive the vagaries of an old man
yeah... you were prompted to press any key twice because you wrote system ("pause") in your code.. this makes your code paused till you press any key.. so you had to press key 2 times one to come out from that pause and then another to terminate the program....