### am i doing this right?

im not sure if im calling the bool function correctly or if something is messed up in the function itself or the main function.
this is the bool function:
 ``1234567891011121314151617181920212223242526272829`` ``````bool aretheyvalid (int x, int y, int z) { int invalid=0, valid=0; bool ans; if (x && y && z >= 0 && x && y && z <= 100) { valid++; ans=true; } else { if (x < 0) cout<< x << " is too small" << endl; if (y < 0) cout<< y << " is too small" << endl; if (z < 0) cout<< z << " is to small" << endl; if (x >100) cout<< x << " is too big" << endl; if (y > 100) cout<< y << " is too big" << endl; if (z > 100) cout<< z << " is too big" << endl; ans=false; } invalid++; return ans; return valid; return invalid; }``````

this is the part of the main function with bool;
 ``12345678`` ``````bool ans; ans= aretheyvalid(test1, test2, test3); if (ans) { cout<< "this group is valid" << endl; } else { cout<< "this group is invalid" << endl; }``````

the too small and too big part work fine in the bool function. the main function doesnt seem to be working right. i need it to say invalid when x, y, or z are above 100 or below 0. thank you
 ``12345`` `````` invalid++; return ans; return valid; return invalid;``````

So, your flow of execution reaches `invalid++; `, and does that (i.e. increments the value of invalid) and then reaches `return ans;`, so the fnuction ends, and returns the value `ans` to the calling function.

 ``12`` `````` return valid; return invalid;``````
These will never ever be executed, because the function has already ended. What are you trying to do here?

`if (x && y && z >= 0.... `
This is just wrong. You've clearly said to yourself something like:
"if x and y and z are greater than zero"
and tried to convert English into C++. That won't work. You can't translate English into C++.
I expect you meant this:
 ``123456`` ``````if (x >=0 && // if x is bigger than or equal to zero AND y > = 0 && // if y is bigger than or equal to zero AND z >= 0 && // if z is bigger than or equal to zero AND x <= 100 && // if x is smaller than or equal to one hundred AND y <= 100 && // if x is smaller than or equal to one hundred AND z <= 100) // if x is smaller than or equal to one hundred ``````

Last edited on
thanks a lot.
Topic archived. No new replies allowed.