0

I'm trying to make a really basic calculator program. I am getting the following error message:

Cannot implicitly convert type 'bool' to 'string'

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2019_03_21
{
    class Program
    {
        private static double Negyzet(int alap, int kitevo)
        {
            Console.WriteLine("Kérem a hatvány alapját!");
            alap = int.Parse(Console.ReadLine());
            Console.WriteLine("Kérem a hatvány kitevojet!");
            kitevo = int.Parse(Console.ReadLine());
            return Math.Pow(alap, kitevo);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Kérem adja meg milyen műveletet szeretne elvégezni!\n\n+ összeadás\n- kivonás\n* szorzás\n/ osztás\n^hatványozás\n\nVálasztott művelet:");
            string muvelet = Console.ReadLine();
            switch (muvelet)
            {
                case (muvelet == "^"): Console.WriteLine("A hatvány értéke:     {0}", Negyzet(0, 0)); break;
                default: break;
            }
            Console.ReadKey();
        }
    }
}
4
  • 4
    See the documentation. Commented Mar 21, 2019 at 20:18
  • 2
    @Symon From the documentation: If expr and constant are integral types, the C# equality operator determines whether the expression returns true (that is, whether expr == constant). Otherwise, the value of the expression is determined by a call to the static Object.Equals(expr, constant) method. Commented Mar 21, 2019 at 20:27
  • Well, the basic difference of if and switch is that the latter might make use of a lookup table, if possible, which is much faster when many clauses are needed, while the former always evaluates the clauses consecutively. Commented Mar 21, 2019 at 20:31
  • 1
    so again, thank you guys for the answers! Commented Mar 21, 2019 at 20:32

3 Answers 3

3

muvelet is a string while muvelet == "^" is a comarision which is boolean (it is either true or false

switch(muvelet)  
{
     case "^":
         // code for when it is equal to "^"
          break;
    //other cases
    default:
         //it is unknown character
}

note that the type in your switch (that is a string in this case) should match the type of cases

Sign up to request clarification or add additional context in comments.

Comments

3

You're using the case clause in a wrong way. It expects integer or String values - but you supply a Boolean value. It's easy to fix that, however. Just write the case clause like that:

case "^":

Then it should compile and work as expected.

Comments

0

Until C# 6, the switch instruction was reserved for primitive types. You can now switch to patterns.

Pattern Matching

And so you can do that kind of thing :

class Program
{
    static void Main(string[] args)
    {
        Print("+", 2, 2);
        Print("-", 2, 2);
        Print("/", 2, 2);
        Print("*", 2, 2);
        Print("^", 2, 2);
        Print("%", 2, 2);
        Print(" ", 2, 2);
        Print("", 2, 2);
        Console.Read();
    }
    static void Print(string op, int nmb1, int nmb2)
    {
        var res = Compute(op, nmb1, nmb2);
        Console.WriteLine(res != null ?
            $"{nmb1} {op} {nmb2} = {res}" :
            $"invalid {op?.Trim()} operator description");
    }

    static int? Compute(string op,int nmb1,int nmb2)
    {
        switch (op)
        {
            case "+":
                return nmb1 + nmb2;
            case "-":
                return nmb1 - nmb2;
            case "*":
                return nmb1 * nmb2;
            case "/":
                return nmb1 / nmb2;
            case "%":
                return nmb1 % nmb2;
            case "^":
                return nmb1 ^ nmb2;
            case var o when (o?.Trim().Length ?? 0) == 0:
                // white space
                return null;
            default:
                return null;
        }
    }
}

console output :

2 + 2 = 4
2 - 2 = 0
2 / 2 = 1
2 * 2 = 4
2 ^ 2 = 0
2 % 2 = 0
invalid operator 
invalid operator 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.