3

I am new to C# and I am trying to make a calculator. In Python(which I am more familiar with), You just import mathand then write out what it is you want to do with math.

But with C#, this is my code:

using system;


namespace Calculator
{
    class MainClass
    {
        public static void Main(string[] args)
        {
             divide(2,3);
        }
        public static void add(int num01, int num02)
        {
            Console.WriteLine("The result is " + num01+num02);
            Console.ReadKey();
        }
        public static void multiply(int num01, int num02)
        {
            Console.WriteLine("The result is " + num01 * num02);
            Console.ReadKey();
        }
        public static void divide(double num01, double num02)
        {
            Console.WriteLine("The result is " + num01 / num02);
            Console.ReadKey();
        }
        public static void subtract(int num01, int num02)
        {
            Console.WriteLine("The result is " + num01 - num02);
            Console.ReadKey();
        }
    }
}

And it firstly gives me 23 if I try to add, and throws a syntax error( Operator '-' cannot be applied to operands of type 'string' and 'int'.) if I try to subtract.

I'm only new to this language so I'm probably making some silly mistakes.

Majid Parvin
  • 4,499
  • 5
  • 29
  • 47
Jargon57
  • 67
  • 2
  • 6
  • 1
    You're adding them as `string`. You need to get the values as `int` first. – Kyle Jun 09 '17 at 16:23
  • 1
    `"The result is " + num01 - num02` is like `("The result is " + num01) - num02`; the number gets concatenated to the string first, and then the subtraction happens. (It associates left-to-right.) Use parentheses to change that. – Ry- Jun 09 '17 at 16:24
  • Console.WriteLine("The result is " + (num01+num02));`` – Rohit Jun 09 '17 at 16:26
  • Is there any language that computes `"Result = " + 4 -2"` as "Result = 2"? – Alexei Levenkov Jun 09 '17 at 16:57
  • see: https://stackoverflow.com/questions/234217/is-it-possible-to-compile-and-execute-new-code-at-runtime-in-net – Greg Jun 09 '17 at 17:37

4 Answers4

6

This mix-up comes from the confusion between two roles of +:

  • When it is used in an expression with strings, it denotes concatenation
  • When it is used in an expression with numeric types, it denotes addition

You can fix this problem by placing parentheses around your expressions.

However, a better approach is to use string formatting or string interpolation instead of concatenation, which lets you avoid this problem altogether:

Console.WriteLine("The result is {0}", num01 - num02); // Formatting

or

Console.WriteLine($"The result is {num01 - num02}"); // Interpolation
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4

convert your final calculation to a string like so.

Console.WriteLine("The result is " + (num01 - num02).ToString());

Or just wrap in parenthesis

Console.WriteLine("The result is " + (num01 - num02));
hack3rfx
  • 573
  • 3
  • 16
0

just put in order your operations with ().

namespace Calculator
{
    class MainClass
    {
        public static void Main(string[] args)
        {
             divide(2, 3);
        }
        public static void add(int num01, int num02)
        {
            Console.WriteLine("The result is " + (num01 + num02));
            Console.ReadKey();
        }
        public static void multiply(int num01, int num02)
        {
            Console.WriteLine("The result is " + (num01 * num02));
            Console.ReadKey();
        }
        public static void divide(double num01, double num02)
        {
            Console.WriteLine("The result is " + (num01 / num02));
            Console.ReadKey();
        }
        public static void subtract(int num01, int num02)
        {
            Console.WriteLine("The result is " + (num01 - num02));
            Console.ReadKey();
        }
    }
}
Majid Parvin
  • 4,499
  • 5
  • 29
  • 47
0

Order of Operations is really important in strongly typed languages like Java and C#. Like many already answered you are first adding the string to the first number and then trying to subtract from the string.

This is what your code is doing:

1. "The result is " + num01 - num02
2. "The result is (value of num01)" - num02
3. Error when trying to subtract

By changing it to this

Console.WriteLine("The result is " + (num01 - num02));

Your code executes like this:

1. "The result is " + (num01 - num02)
2. "The result is " + (difference of num01 and num02)
3. "The result is (difference of num01 and num02)"

Hope this helps

Deric Plummer
  • 117
  • 1
  • 3
  • 10