Hi this is probably a pretty basic question, but i am trying to create a program that will find out if 3 numbers are all unique. I am trying to do this by using a bool function and a main function. Below is the code i have writen so far.
#include <iostream>
using namespace std;
bool is_different(int num1,int num2,int num3);
void main()
{
int num1,num2,num3;
do
{
cout << "Please enter three values" <<endl;
cin >> num1 >> num2 >> num3;
is_different (num1, num2, num3);
if (is_different)break;
}
}
bool is_different(int num1,int num2,int num3)
{
return (num1 != num2 && num1 != num3 && num2 != num3);
}
My problem is such that i am getting a error saying that there is a syntax error
for the bracket just about the bool is_different (-----) function. everything i have tried cannot seem to get rid or this syntax error. If possible could a reason be given for whats wrong with it and how to fix the error.
Thanks Sean.
OK, first of all, stop using void main, because void main is stupid. main is supposed to be int and all other uses are incorrect. Second, that could use code tags, although it's short enough that I can live without them.
Now then, I don't really see what the point is of that break, and you can't test a function like that. I think you mean to determine if isdifferent is true, right? In that case you need to have this: if (is_different(num1,num2,num3)
You cannot perform a true-false test on the name of a function. It has no value. That's probably your error.
sorry i originally had the if statment like below
bool is_different(int num1,int num2,int num3)
{
if (num1 != num2 && num1 != num3 && num2 != num3)
{
return true;
}
else
return false;
}
but i had read online that this a extraneous for a bool statement so i changed it, i have changed it back to this and include a int main instead of void, and am still getting the same problem " error C2059: syntax error : '}' "
Bellow is the updated code
#include <iostream>
using namespace std;
bool is_different(int num1,int num2,int num3);
int main()
{
int num1,num2,num3;
do
{
cout << "Please enter three values" <<endl;
cin >> num1 >> num2 >> num3;
is_different (num1, num2, num3);
if (is_different)break;
Well since the function is_different returns a bool value you should assign it to something before you put it in the if statement. You could also put the function in the if statement but you must give it some arguments (num1,num2,num3). So basically here is what i meant:
#include <iostream>
using namespace std;
bool is_different(int num1,int num2,int num3);
int main()
{
int num1,num2,num3;
bool TrueORFalse=false;
while(1)
{
cout << "Please enter three values" <<endl;
cin >> num1 >> num2 >> num3;
TrueORFalse=is_different (num1, num2, num3);
if (TrueORFalse) cout<<"The numbers are equal\n";
else cout<<"You have entered different numbers\n";
}
Well you don't have a while closing that loop so what is there to break out of? That's illegal loop syntax.
And I'm going to say it again: isdifferent means NOTHING. The effect of the function depends on the parameters it is passed. There is no persistence or holdover data between calls of that function. If you want to test the value, you need to perform an actual CALL of the function, not perform a damn impossible test on the function's meaningless valueless name. That's why you can't do if (is_different) // is_different is a function and CANNOT BE TESTED
Also, that break is probably not necessary. You have better ways to do it than to have an infinite loop with a break.. but I'm not going to go into them because you honestly don't seem to exhibit understanding of either, and I'll only confuse you further.