Will someone look this code over?

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

error C4716: 'isValid' : must return a value

Thanks,


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
 using namespace 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.
Last edited on
I'm no expert, but you set isValid to return int, yet you didn't return anything.

Change "int isValid" to "void isValid".
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..
Last edited on
The OP is using the "triangle inequality"
Last edited on
.....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))
Last edited on
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.
Thanks for the help everyone, unfortunately I am only on apple machines today and cant access my windows box with visual. So I need to change my...

int isValid

on both line 4 and 31 to...

void isValid

and is that it? I am trying to get it all sorted out so it will run fine when I can get to my windows machine. thanks again everyone!
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.
ok heres what I have so far....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
 using namespace 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
Just call it in main under your cin >> c; code:
isValid(a, b, c);
http://www.cplusplus.com/doc/tutorial/functions/
why do I have to

press any key to continue:


twice after my results are displayed?
Given the code you posted, that is not possible, unless you got a virus in one of the hundred billion programs that system("pause") calls.
when I took the system pause out it only prompt me once with that. I guess I should jsut leave it out?
Here is the final code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
 using namespace 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;  

 isValid(a, b, c);


 return 0;

 } //end main
   
 // Call isValid function: 
 void isValid (int tri_a, int tri_b, int tri_c)  

  // Determine if  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;
 } 
@ 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?
Sure, that logic works.

this:

1
2
3
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).

That's a world different than this

buffbill wrote:
if ((tri_a + tri_b > tri_c) || (tri_a + tri_c > tri_b) || (tri_b + tri_c > tri_a))


which would be applied to this code

1
2
3
4
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;


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.
Last edited on
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....
Topic archived. No new replies allowed.