14,929,005 members
See more:
Hi I get an error:
Cannot implicitly convert type 'bool' to 'int'
anyway the program executes with the errors but I dont know how to fix it.
Here is the code snippet:
```// switch on the value of inputInt
switch (inputInt)
{
case (inputInt == 0):
goto repeat;
case (inputInt % 2 != 0):
goto repeat;
case (inputInt % 2 == 0):
goto repeat;
case (inputInt % 10 == 0):
goto repeat;
case (inputInt > 100):
goto repeat;
}```

I do not understand what is the problem. Can't we have formulas and equations into the case statement?
Best regards

It is a schools assignment.
Task number one was to do this using if-else statements, it is ok.
Task number two is to do the same program, using a switch statement...
It is going to be more code, just to accomplish it with a switch statement.

Otherwise I also don't thing it is efficient to use a switch statement here.

1. "Create a program that prompts user for an input, accepts an integer, then evaluates whether that input is zero, odd or even, a multiple of 10, or too large (more than 100) by using multiple levels of 'if' statements."

2."Rewrite the the program from the previous exercise to do the same work with a switch statement...

-----------------------------------------------------------------

why do you nest them?

i wast thinking of a way to have to outputs with a switch statement.
for example a number that is multiple of 10 is also an even number.
so it is gonna be nice to have to console outputs:
"the num is multiple of 10"
"the num is even"
but i got realy bored for today, and I suspect it is not possible only with a switch statements, even nested ones.
Posted
Updated 11-Mar-10 10:21am
v3

## Solution 1

If `inputInt` is an `int`, then the `case` values should be `int` `const`s, ex: `0`, `1`, `10`..., not `bool` (`true` of `false`) expressions.
v2

## Solution 2

Toli is exactly right. Your switch statement is told to evaluate an `Int`. Think of the switch as an easier to write series of `If` `Then` statements. In your case, it would equate to:
C#
```if (inputInt == (inputInt == 0))
{
...;
}
else if (inputInt == (input %2 != 0))
{
...;
}
... ```

So, it fails because you're asking it if an `int` is equal to a `boolean` value.

From msdn:
"In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. In C#, there is no conversion between the bool type and other types. For example, the following if statement is invalid in C#:"

## Solution 6

Your basic issue is that you don't understand switch statements. A switch statement is not a case. To do it with a case statement you'd need to do this:

C#
```switch (inputInt)
{
case 0:
break;
case 2:
case 4:
case 6:
case 8: // etc
break;
//etc```

I agree, this is not a good place to use a case statement, but this is almost certainly what your teacher wants to see. The important points:

1 - a case statement means you list the cases you want to catch, a case is NOT a place to write evaluative code, as you did
2 - there is no reason to use goto here, or, basically, almost anywhere. I have never used goto in my code. It makes your code unreadable and is unnecessary.
3 - you can put more than one case in a block, so I did 2,4,6,8, etc, then put the one block of code that executes for all those cases.

I should add a couple of things:

1 - we don't normally do people's homework. I was happy to help you b/c you were trying to do your own work, not just asking for a solution, but it does seem to me like you were struggling a bit with the way case statements work, and I'd recommend you also talk to your teacher about this, to make sure it's clear in your mind.

2 - you should not push the 'answer' button to ask more questions. You should edit your post ( I copied your replies into your main post ), or use the forum under the answers section.
v2

## Solution 3

It is a schools assignment.
Task number one was to do this using if-else statements, it is ok.
Task number two is to do the same program, using a switch statement...
It is going to be more code, just to accomplish it with a switch statement.

Otherwise I also don't thing it is efficient to use a switch statement here.

1. "Create a program that prompts user for an input, accepts an integer, then evaluates whether that input is zero, odd or even, a multiple of 10, or too large (more than 100) by using multiple levels of 'if' statements."

2."Rewrite the the program from the previous exercise to do the same work with a switch statement...

## Solution 4

God...yeah, that's not really a prime case for a switch statement in my opinion.

The best, I could think of doing would be nested switch statements like:

C#
```switch (n)
{
//check if zero
case 0:
Console.WriteLine("Is Zero");
break;
//not zero
default:
//check if it's greater than 100
//Math.Floor will round down (so 0 if less than 100)
switch ((int)Math.Floor((double)(n / 100)))
{
//less than 100
case 0:
//run a mod 10 (0 is multiple of 10)
switch (n % 10)
{
//is a multiple of 10
case 0:
Console.WriteLine("Is a multiple of 10");
break;
//check if odd
case 1:
case 3:
case 5:
case 7:
case 9:
Console.WriteLine("Is Odd");
break;
//even
default:
Console.WriteLine("Is Even");
break;
}
break;
default:
Console.WriteLine("> 100");
break;
}
break;
}```

[Modified]
If you want to also output "Is even" if it's a multiple of 10, then just add another `Console.WriteLine(...)`
v4

## Solution 5

why do you nest them?

i wast thinking of a way to have to outputs with a switch statement.
for example a number that is multiple of 10 is also an even number.
so it is gonna be nice to have to console outputs:
"the num is multiple of 10"
"the num is even"
but i got realy bored for today, and I suspect it is not possible only with a switch statements, even nested ones.

## Solution 7

```switch (n % 10)
{
//is a multiple of 10
case 0:
Console.WriteLine("Is a multiple of 10");
break;
//check if odd
case 1:
case 3:
case 5:
case 7:
case 9:
Console.WriteLine("Is Odd");
break;
//even
default:
Console.WriteLine("Is Even");
break;
}
```

You tricked it man! That is why there is an additional requirement to evaluate for a max value (is > 100).
Then for (input % 10) when (input > 100) we have a limited number of cases: case 1,3,5,7,9. The modulus is an odd number, so odd is the dividend.
Thank you for helping me understand the switch statement logics.
Best Regards
P.S. as about if the assignment is crazy - it is challenging to explore the language's features.
Otherwise this particular solution (switch) is maybe not pragmatic to quick solve a real task.
v2