Home > Software engineering >  How would i allow the user to input a symbol like " " or "-" and allow that to b
How would i allow the user to input a symbol like " " or "-" and allow that to b

Time:10-10

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter your first number: ");
            int num1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("would you like to Subtract(-), add( ), multiply(*) or divide(/), you must enter a symbole or it will not work");
            string symbol = Console.ReadLine();

            Console.Write("Enter your second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());

            
        }
    }
}

I want it to use the user input of either , -, / or * to add the numbers the user has chosen

CodePudding user response:

You could use a string and from there write your own code that looks for operators and do the expected calculation.

CodePudding user response:

You could use a switch statement to select the right operation. Don't make it too complex. You'll need to add some code such as trimming. For example instead of ' ', you'll enter ' '

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter your first number: ");
            int num1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("would you like to Subtract(-), add( ), multiply(*) or divide(/), you must enter a symbole or it will not work");
            string symbol = Console.ReadLine();

            Console.Write("Enter your second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());

            switch (symbol)
            {
                case " ":
                    Console.Write($"The anser is {num1   num2}");
                    break;

                case "-":
                    Console.Write($"The anser is {num1 - num2}");
                    break;

                case "*":
                    Console.Write($"The anser is {num1 * num2}");
                    break;

                case "/":
                    Console.Write($"The anser is {num1 / num2}");
                    break;

                default:
                    Console.Write($"Too bad '{symbol}' is not implemented");
                    break;
            }
        }
    }
}
  • Related