Kaspar Tvedt Moberg
Kaspar Tvedt Moberg

Reputation: 27

Operator "||" cannot be applied to operands of type 'bool' and 'int'

I'm quite new to C# and trying to make text base mastermind, but when i try to check if the users answer is the same as 3 of the numbers i get this error. "Operator "||" cannot be applied to operands of type 'bool' and 'int'"

        Random rnd = new Random();

        int pos1 = rnd.Next(1, 6);  //generates random numbers
        int pos2 = rnd.Next(1, 6);
        int pos3 = rnd.Next(1, 6);
        int pos4 = rnd.Next(1, 6);

        int answer1 = Convert.ToInt32(Console.ReadLine());

        if (asnwer1 == pos1)           //checks if answer is the same as pos1
        {
            Console.WriteLine("Right");
        }
        else if (answer1 == pos2 || pos3 || pos4)
        {
            Console.WriteLine("Wrong");
        }
        else {
            Console.WriteLine("Nope");

Upvotes: 1

Views: 3583

Answers (5)

I assume that, by

 else if (answer1 == pos2 || pos3 || pos4)

you meant "answer1 equals pos2, pos3, or pos4".

However, this isn't the correct syntax for that. As the error message states, it's expecting everything inside || to be a Boolean expression, and pos3 and pos4 definitely are not Boolean.

You could write an extension method like this:

public static class Extensions
{
    public static bool In<T>(this T item, params T[] args)
    {
        foreach (T arg in args)
        {
            if (arg.Equals(item))
                return true;
        }

        return false;
    }
}

Or, more simply:

public static class Extensions
{
    public static bool In<T>(this T item, params T[] args)
    {
        return args.Contains(item);
    }
}

You could then write answer1.In(pos2, pos3, pos4).

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

Do

else if (answer1 == pos2 || answer1 == pos3 || answer1 == pos4)

Ideally your code should be

Console.WriteLine(answer1 == pos1 ? "Correct": "Incorrect");

Upvotes: 0

mrogal.ski
mrogal.ski

Reputation: 5940

Logical operators such as || requires from you to have boolean value on both sides left || right. In your example you have first operand of type bool and the next ones are integers. Which does not make any sense.

To be able to compare if your answer is equal to these three values you have to use them in a condition-like statement ( answer == posX ) where X is your position's number.

Example of working code of yours :

else if (answer1 == pos2 || answer1 == pos3 || answer1 == pos4 )
// do some logic here ...

To extend this into more "generic" way, each operand has to present a boolean value as such :

if ( true || false || ... )

But since it wouldn't be helpfull to pack raw boolean value into this like such :

bool a = answer1 == pos2;
bool b = answer1 == pos3;
if ( a || b ... )

You can actually write conditions like the one you've tried :

if ( operandL == operandR || operandL != operandR || ... )

Upvotes: 0

Matthew Whited
Matthew Whited

Reputation: 22443

if (new[] {pos2,pos3,pos4}.Contains(answer1))

Upvotes: 1

Roman
Roman

Reputation: 12201

All conditions should return bool result. pos3 and pos4 are int - you should compare with answer1.

You should rewrite your else if:

else if (answer1 == pos2 || answer1 == pos3 || answer1 == pos4)

Upvotes: 5

Related Questions