#include<iostream>
#include<fstream>
#include<cstdlib>
usingnamespace 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)
{
returnfalse;
}
elsereturntrue;
}
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
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";
}
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);
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.
#include<iostream>
#include<fstream>
#include<cstdlib>
usingnamespace 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;
}
elsereturn triangle = true;
}
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)?
#include<iostream>
#include<fstream>
#include<cstdlib>
usingnamespace 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))
{
returnfalse;
}
elsereturntrue;
}
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