how to use the bool operator??

im having trouble using the bool operator in my program. for my program i have to use a bool function to determine whether three integers that represent three test scores are valid or not. the scores have to be from 0-100 including 0 and 100. if the score is invalid, the function will print a message explaining its invalid. if all three are valid, the function will return a signal to the main program saying this is a valid set of scores. and if any one of the three scores is invalid the function will return a signal saying this is an invalid set of scores. i tried many different things but none seem to work right. how am i supposed to do this. i tried something but i know its wrong but im not sure how to fix it. thanks for helping. this is what i put in the main:
1
2
3
4
5
6
7
  
        bool ans;
        ans= aretheyvalid(x, y, z);
        if (ans==true)
            cout<< "this group is valid" << endl;
        else
            cout<< "this group is invalid" << endl; 


and this is the function:
1
2
3
4
5
6
7
bool aretheyvalid (int x, int y, int z) {
    bool ans; 
    if (x >= 0 && y >= 0 && z >= 0 && x <= 100 && y <= 100 & z <= 100)
        ans=true;
    else
        ans= false;
    return ans;
Last edited on
 
if (x >= 0 && y >= 0 && z >= 0 && x <= 100 && y <= 100 & z <= 100)


You forgot an ampersand at the end there.

Also, remember that you can just return the value of the bool expression, you don't need an intermediate variable:

1
2
3
4
bool aretheyvalid(int x, int y, int z)
{
return (x >= 0 && y >= 0 && z >= 0 && x <= 100 && y <= 100 && z <= 100);
}


You can do the same thing with your if statement:
 
if (aretheyvalid(x,y,z))
sorry im confused. i added more to the bool function but it still doesnt work right. if doesnt do the anything after else. it keeps telling me they are all valid even when i type in a number that is bigger than 100.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool aretheyvalid (int x, int y, int z) {
    int invalid=0;
    bool ans; 
    if (x >= 0 && y >= 0 && z >= 0 && x <= 100 && y <= 100 & z <= 100)
        ans=true;
    
    else {
        ans= false;
        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;
        invalid++; }
    return ans;
    
}
The function looks fine (other than the fact that you still have a bitwise and in there), so the problem must be in how you read the values or how you call the function.
Look at your function definition for aretheyvalid:
1
2
3
4
5
bool aretheyvalid (int x, int y, int z) {
    int invalid=0;
    bool ans; 
    if (x >= 0 && y >= 0 && z >= 0 && x <= 100 && y <= 100 & z <= 100)
        ans=true;

You have a bitwise and operator &, not a logical and && when you refer to z <= 100! That's the only problem, make sure you know the difference.
got it. thanks.
Topic archived. No new replies allowed.