Bool functions

Nov 11, 2014 at 5:20pm
Im having an issue creating a bool function. I don't understand why its not working right when the logic seems correct.

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
40
41
42
43
  #include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

bool isValidTriangle(int a, int b, int c);
int main()
{
	int a, b, c;
	ifstream edges;
	edges.open("edges.txt");
	if (edges.is_open())
	{
		while (edges >> a)
		{
			edges >> b
				>> c;
			bool isValidTriangle(int a, int b, int c);
			if (true)
			{
				cout << "triangle\n";
			}
			else cout << "not\n";
			}

	}
	else
	{
		cout << "Input file open failed.\n";
		exit (1);
	}
	edges.close();
	return 0;
}

bool isValidTriangle(int a, int b, int c)
{
	if ((a+b) < c)
	{
		return false;
	}
	else return true;
}


I get triangle, triangle, triangle...ect
Nov 11, 2014 at 5:31pm
Please post a sample of your input file.

Nov 11, 2014 at 5:32pm
What are the values of a, b and c?

Also, you can write:
 
while (edges >> a >> b >> c)
Nov 11, 2014 at 5:33pm
What do line 6 and 18 have in common? The reason why you are getting the output you get is because your if-statement is always true (line 19):
if(true) // Yes this is true...always

You should replace true with something that will maybe evaluate to false? I have a feeling you are trying to evaluate your function, so you should be replace that true with your function call
Nov 11, 2014 at 5:38pm
closed account (SECMoG1T)

From line 14 to 24

1
2
3
4
5
6
7
8
9
10
11
12
13
		
   while (edges) 	
	{ 			
              edges >>a>> b >> c; 			
               ///bool isValidTriangle(int a, int b, int c); 	this is a redeclaration of your function
		if (isValidTriangle (a, b, c)) /// I guess this is what you wanted to do
       		  { 	
                      cout << "triangle\n"; 			
                  } 			

               else
                     cout << "not a triangle\n"; 			
        } 	
Last edited on Nov 11, 2014 at 5:45pm
Nov 11, 2014 at 5:39pm
You're not suppose to put "Bool" in front of the function you are calling! Only upon creation or definition. Same goes with "Int a, int b, int c,"
bool isValidTriangle(int a, int b, int c);


Here is what you call:
isValidTriangle(a,b,c);
Last edited on Nov 11, 2014 at 5:44pm
Nov 11, 2014 at 5:52pm
Sorry, I forgot to add in my input from my file. I am trying to get my function to put out an evaluation that would set off the if and else statements. Heres the updated version and still get the same results.

The file is:

12 15 7
8 9 14
11 2 15
6 5 4
18 7 10
16 11 17


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
40
41
42
43
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

bool isValidTriangle(int a, int b, int c);
int main()
{
	int a, b, c;
	bool triangle = true;
	ifstream edges;
	edges.open("edges.txt");
	if (edges.is_open())
	{
		while (edges >> a >> b >> c)
		{
			isValidTriangle(a,b,c);
			if (triangle==false)
			{
				cout << "not\n";
			}
			else cout << "triangle\n";
		}

	}
	else
	{
		cout << "Input file open failed.\n";
		exit (1);
	}
	edges.close();
	return 0;
}

bool isValidTriangle(int a, int b, int c)
{
	bool triangle = true;
	if ((a+b) < c)
	{
		return triangle = false;
	}
	else return triangle = true;
}
Last edited on Nov 11, 2014 at 5:55pm
Nov 11, 2014 at 6:03pm
Line 17: You're not saving the result of the function call.

 
  triangle = isValidTriangle(a,b,c);


Or simply use the function call in your if statement and you don't need the triangle variable at all. Delete line 17. Replace line 18:
 
  if (! isValidTriangle(a,b,c))


BTW, get rid of the else on line 22. Otherwise, you're just going to print only the word "not".

Lines 40, 42: There's no need to assign true or false to trinagle. Simply return true or false directly.
Last edited on Nov 11, 2014 at 6:07pm
Nov 11, 2014 at 6:24pm
Ok, I got it to work thank you for your help! I didn't think to use the function in the if statement like that. Last question that I have. The if statement: if (! isValidTriangle (a,b,c)) that reads "not" isValidTriangle (a,b,c)? Meaning false isValidTriangle(a,b,c)?

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
40
41
42
43
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

bool isValidTriangle(int a, int b, int c);
int main()
{
	int a, b, c;
	ifstream edges;
	edges.open("edges.txt");
	if (edges.is_open())
	{
		while (edges >> a >> b >> c)
		{
			if (isValidTriangle(a, b, c))
			{
				cout << "triangle\n";
			}
			if (!isValidTriangle(a, b, c))
			{
				cout << "not\n";
			}
		}

	}
	else
	{
		cout << "Input file open failed.\n";
		exit (1);
	}
	edges.close();
	return 0;
}

bool isValidTriangle(int a, int b, int c)
{
	if (((a + b) < c) || ((b+c)<a))
	{
		return false;
	}
	else return true;
}
Nov 11, 2014 at 6:49pm
The if statement: if (! isValidTriangle (a,b,c)) that reads "not" isValidTriangle (a,b,c)? Meaning false isValidTriangle(a,b,c)?

Correct.

BTW, you don't need to repeat the test for the negative at line 20. Simply use else.

When I suggested getting rid of the else, you had the false condition first. That would result if outputting the word "not" if it was not a triangle, followed by always outputting the word "triangle".

1
2
3
    if (! isValidTriangle(a, b, c))
      cout << "not ";
    cout << "triangle";  // output after not or by itself 



Nov 11, 2014 at 7:20pm
One more thing:

You're only comparing to find if a+b < c; what if a + c < b, or b + c < a?

You can simplify your function too:

1
2
3
4
5
6
bool isValidTriangle( int a, int b, int c)
{

    return ( a+b > c &&  . . . && . . .)
// fill in the ... with other conditions as required :)
}
Last edited on Nov 11, 2014 at 7:37pm
Nov 11, 2014 at 8:24pm
Thanks! I changed it up since if it is a triangle then it would have to be &&.
Topic archived. No new replies allowed.