-2

I'm writing a program that lets you input two numbers and gives you all the math answers for it. So like multiplication, division, etc. Problem is when I run it, it asks for the input and then it just shows all the operator strings but empty and then ends there.

I tried making an array and then setting that equal to the operators in the Console.WriteLines but that didn't work.

static void Main(string[] args)
{
    Console.WriteLine("Input any two numbers.");

    var number1 = int.Parse(Console.ReadLine());
    var number2 = int.Parse(Console.ReadLine());

    Console.WriteLine("Addition: ", + (number1 + number2));
    Console.WriteLine("Division: ", + (number1 / number2));
    Console.WriteLine("Subtraction: ", + (number1 - number2));
    Console.WriteLine("Multiplication: ", + (number1 * number2));
}
mjwills
  • 23,389
  • 6
  • 40
  • 63
PetiteMeat
  • 11
  • 2
  • 2
    What do you expect the result of 3 / 2 to be? – mjwills Jul 12 '19 at 22:47
  • 2
    While we are looking at your code: use `TryParse`, not `Parse`. `Parse` is for the situation where you *know* that the text will *always* be a number, but you do not know what the user is going to type. – Eric Lippert Jul 12 '19 at 22:47
  • Consider also adding the `%` operator; today would be a good day to learn about it if you have not already. – Eric Lippert Jul 12 '19 at 22:48
  • I was surprised the code in the question even compiled with the combination of a `,` immediately followed by `+`. Today is evidently a good day to learn about the [unary plus operator](https://stackoverflow.com/q/727516/150605). – Lance U. Matthews Jul 12 '19 at 22:51
  • 1
    @BACON: It is my least favourite operator; it has almost no legitimate uses, but it produces opportunities like this for errors and adds work for the compiler developers. But it turns out there are a lot of weird operators in C#. For example, did you know that we made `while (x --> 0)` an operator that means "loop x from its current value down to zero" ? – Eric Lippert Jul 12 '19 at 23:08

2 Answers2

9

You wrote:

Console.WriteLine("Addition: ", + (number1 + number2));

You meant to write

Console.WriteLine("Addition: " + (number1 + number2));

Note that you added an extra comma.

This code is legal, but bad style. See below for better ways to write this.

What is the meaning of the code you wrote? Console.WriteLine lets you do this:

Console.WriteLine("Addition:{0} + {1} = {2}", number1, number2, number1 + number2);

That is "replace {0} with the first thing following, {1} with the second thing, and so on".

So the code you wrote was "replace {0} with the value of +(number1+number2), but you don't have a {0}, so, nothing happens.

Today would be a great day for you to learn about interpolated strings:

Console.WriteLine($"{number1} + {number2} = {number1 + number2}");

Notice the $ that indicates that the string is interpolated; expressions inside {} will be evaluated and turned into text.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
2

Concatenation with string returns string but you have to write it like that: Console.WriteLine("Division: "+(number1 / number2));

You can also use placeholder Console.WriteLine("Division: {0}",(number1 / number2));

or string interpolation Console.WriteLine($"Division: {number1 / number2}");

73s73r
  • 21
  • 4