4

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.

5
  • 1
    You're adding them as string. You need to get the values as int first. Commented Jun 9, 2017 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. Commented Jun 9, 2017 at 16:24
  • Console.WriteLine("The result is " + (num01+num02));`` Commented Jun 9, 2017 at 16:26
  • Is there any language that computes "Result = " + 4 -2" as "Result = 2"? Commented Jun 9, 2017 at 16:57
  • see: stackoverflow.com/questions/234217/… Commented Jun 9, 2017 at 17:37

4 Answers 4

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 
Sign up to request clarification or add additional context in comments.

Comments

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)); 

1 Comment

You don't have to use .ToString()
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

Comments

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(); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.