C# Error Concatenation instead of calculating

Guys please help I’ve been trying to build a calculator like app , but instead of calculating the two variables it keeps concatenating them (I.e int num 1 =9; int num 1 =9; … num1 + num2 prints 99 instead of 18) , any help would kindly really be appreciated.

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter two numbers");
            string num1 = Console.ReadLine();
            Convert.ToInt16(num1);
            string num2 = Console.ReadLine();
            Convert.ToInt16(num2);
            String m = "for multiplying numbers";
            String s = "for subtracting numbers";
            String a = "for adding numbers";
            String d = "for dividing numbers";
            Console.WriteLine("Please Select from one of the options below\n"+ "m"+ m +"\ns " + s + "\nd " + d + "\na " + a);
            String option = Console.ReadLine();
            if (option == "m")
            {
                Console.WriteLine( "The result is " + num1 + num2);
                return;
            } else if (option == "d") {
                Console.WriteLine(num1);
                return;
            } else if (option == "s")
            {
                Console.WriteLine(num2);
                return;
            } else if (option == "a") {
                Console.WriteLine(num2 + num1);
                return;
            }
            Console.ReadKey();
        }

    }
}

Hi!

Because num1 and num2 are both strings, they become concatenated instead of the actual calculation, if you want to do calculations on them, you’ll need to cast them into something like an int instead, then cast it back to a string so you can print it :slight_smile:

-Lyall

1 Like