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:
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
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;
1
2
3
4
5
6
7
8
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
1
2
3
4
5
 
 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.

1
2
  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:
1
2
3
4
5
6
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.