Hi,

Can anyone please tell me how to solve this calculator code with using cases?
I really don't know how to conclude it. It doesn't matter if code is long cuz of cases.

Code:

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

namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            float result;

            try
            {
                Console.Write("Enter the first number: ");
                int a = int.Parse(Console.ReadLine());

                Console.Write("Enter the operation (+), (-), (*) ali (/): ");
                char operation1 = char.Parse(Console.ReadLine());

                if (operation1.Equals('+') || operation1.Equals('-') || operation1.Equals('*') || operation1.Equals('/'))
                {
                    Console.Write("Enter the second number: ");
                    int b = int.Parse(Console.ReadLine());

                    Console.Write("Enter the operation (+), (-), (*) ali (/): ");
                    char operation2 = char.Parse(Console.ReadLine());

                    if (operation2.Equals('+') || operation2.Equals('-') || operation2.Equals('*') || operation2.Equals('/'))
                    {
                        Console.Write("Enter the third number: ");
                        int c = int.Parse(Console.ReadLine());

                        switch (operation1)
                        {
                            case '-':
                                result = a - b - c;
                                break;
                            case '*':
                                result = a * b * c;
                                break;
                            case '+':
                                result = a + b + c;
                                break;
                            case '/':
                                result = a / b / c;
                                break;

                            default:
                                result = 0;
                                break;
                        }
                        Console.WriteLine("Result: " + Math.Round(result));
                    }
                    else
                    {
                        Console.WriteLine("Input error!");
                    }
                }
                else
                {
                    Console.WriteLine("Input error!");
                }
            }
            catch
            {
                Console.WriteLine("Input error!");
            }
            Console.ReadKey();
        }
    }
}

THANK YOU FOR YOUR HELP.

1

Yes I see. I fixed that with just one operators declaration.

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

namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            float result;

            try
            {
                Console.Write("Enter the first number: ");
                int a = int.Parse(Console.ReadLine());

                Console.Write("Enter the operation (+), (-), (*) ali (/): ");
                char operation = char.Parse(Console.ReadLine());

                if (operation.Equals('+') || operation.Equals('-') || operation.Equals('*') || operation.Equals('/'))
                {
                    Console.Write("Enter the second number: ");
                    int b = int.Parse(Console.ReadLine());

                    Console.Write("Enter the operation (+), (-), (*) ali (/): ");
                    operation = char.Parse(Console.ReadLine());

                    if (operation.Equals('+') || operation.Equals('-') || operation.Equals('*') || operation.Equals('/'))
                    {
                        Console.Write("Enter the third number: ");
                        int c = int.Parse(Console.ReadLine());

                        switch (operation)
                        {
                            case '-':
                                result = a - b - c;
                                break;
                            case '*':
                                result = a * b * c;
                                break;
                            case '+':
                                result = a + b + c;
                                break;
                            case '/':
                                result = a / b / c;
                                break;

                            default:
                                result = 0;
                                break;
                        }
                        Console.WriteLine("Result: " + Math.Round(result));
                    }
                    else
                    {
                        Console.WriteLine("Input error!");
                    }
                }
                else
                {
                    Console.WriteLine("Input error!");
                }
            }
            catch
            {
                Console.WriteLine("Input error!");
            }
            Console.ReadKey();
        }
    }
}

Is it a good way to solve that code with all possible switches ?
Example:

switch (operation)
                        {
                            case '-':
                                result = a + b - c;
                                break;
                            case '*':
                                result = a / b * c;
                                break;
                            case '+':
                                result = a - b + c;
                                break;
                            case '/':
                                result = a * b / c;
                                break;

                            default:
                                result = 0;
                                break;
                        }
                        switch (operation)
                        {
                            case '-':
                                result = a / b - c;
                                break;
                            case '*':
                                result = a - b * c;
                                break;
                            case '+':
                                result = a * b + c;
                                break;
                            case '/':
                                result = a - b / c;
                                break;

                            default:
                                result = 0;
                                break;
                        }

Can you give more info about what you are trying to achieve, you are inputting in three numbers, what calculation are you wanting to perform on those numbers?

well you are using "operation1" in a switch statement and the other numbers and operations you enter are not being considered...what are you wanting to achieve with the program?

I just want to make a simple calculator including 3 numbers and 2 operators between them. Basic operators (+), (-), (*) and (/).

is it required that you use switch statements?

No, but it needs to be a simple code. Switch statements are not so complicated.

I've started learning C# about 5 months ago and I don't know a lot of it. So I need to start with the basicaliest syntaxes.

now this is a simple way..there are better ways but hopefully will give you a nice simple understanding...

class Program
    {
        static void Main(string[] args)
        {
            int num1 = 0;
            int num2 = 0;
            int num3 = 0;

            string op1 = null;
            string op2 = null;
            string op3 = null;

int result = 0;//will hold final calculation

            Console.WriteLine("enter first num");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("enter op you want to perform on number");
            op1 = Console.ReadLine();

            Console.WriteLine("enter second num");
            num2 = Convert.ToInt32(Console.ReadLine());

            Console.Write("enter op you want to perform on number");
            op2 = Console.ReadLine();

            Console.WriteLine("enter third number");
            num3 = Convert.ToInt32(Console.ReadLine());

            Console.Write("enter op you want to perform on number");
            op3 = Console.ReadLine();
        }

now when you type your numbers and operations they will be stored in the variables that are declared at the top of the code....

you can then use your if statements to perform actions on these variables..for example

if (op1 == "-")
            {
                result = num1 - num2;
            }

you could then print the result to the console screen as so...

Console.WriteLine("your result is: {0}",result);

does this help you at all?

Yes, this is absolutely better way than switch statements. I'll try to make it.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.