-3

I have a bool function with an if else statements inside it. The first 'if' returns 'false' and the else returns 'true'. I want to call this boolean function in an other function with a while loop inside it. I have tried several times and I made it work. However, when I call the function it return 'true' all the time. How can I fix that?

bool secretCheck(string P1_name, string P2_name, char secret){
    secret =  'R', 'G', 'B', 'P', 'Y', 'M';
    if (secret != 'R' && secret != 'G' && secret != 'B' && secret != 'P' && secret != 'Y' && secret != 'M' && secret > 4){
        return false;   
    }
    else {
        return true;
    }
}

void secretLoop(string P1_name, string P2_name, char secret){
    while(!secretCheck(P1_name, P2_name, secret)){
        cout << "Invalid secret!"<< endl;
        cout << P1_name << ", please enter your secret: ";
        cin >> secret;
    }

    if(secretCheck(P1_name, P2_name, secret)) {
        cout << "Ok\n";
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Yassine Ezzaim
  • 1
  • 1
  • 1
  • 4

2 Answers2

1

I'm unsure what you're trying to do with this line:

secret =  'R', 'G', 'B', 'P', 'Y', 'M';

A char can hold one and only one value. In this case, you're using the comma operator which drops the first value and returns the second expression.

At that line, secret is always equal to 'R', since it's equivalent to say:

secret = 'R'; // now secret is equal to the character 'R'

Then, in your if statement, you got that:

secret != 'R' && /* ... */

This will always be false, since secret is always equal to 'R'.

Then, there's a boguous comparison:

secret > 4

This is also true, since comparing a char and a int will compare the ASCII value of the character.


To store multiple characters in a variable it will either have to be of type std::string or std::vector:

std::vector<char> secret_characters = {'R', 'G', 'B', 'P', 'Y', 'M'};

And if you want to check if secret is in the secret_character list, you can do that:

void is_secret(std::vector<char> const& secret_characters, char secret) {
    auto found = std::find(
        secret_characters.begin(), secret_characters.end(),
        secret
    );
    return found != secret_characters.end();
}

bool secretCheck(string P1_name, string P2_name, char secret){
    std::vector<char> secret_characters = {'R', 'G', 'B', 'P', 'Y', 'M'};
    if (is_secret(secret_characters, secret)) {
        return false;   
    } else {
        return true;
    }
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
0

In your function secretCheck, you assign the value 'R' to secret, completely overwriting whatever it used to be. Then, you return false iff secret != 'R'. Thus, you will never return false, since secret will always be 'R'. It's unclear what you were trying to do in the first line of that function, especially since the meaning of the function is unclear (you never even use either string argument). I would suggest making sure your function is actually performing the task you want it to.

Will Eccles
  • 394
  • 1
  • 5
  • 15