2

So I have almost completed my program but I keep getting this error for the subtraction operator. I have searched throughout my book and the internet but can find no fix for this. Is anybody able to tell me what is wrong with this code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication14
{
class Program
{
    static void Main(string[] args)
    {
        int int1;
        int int2;
        char oper;

        Console.Write("Enter first integer: ");
        int1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter operator (+,-,*, / or %)");
        oper = Convert.ToChar(Console.ReadLine());

        Console.Write("Enter first integer: ");
        int2 = Convert.ToInt32(Console.ReadLine());

        if(oper == '+')
            Console.Write("Answer is: " + int1 + int2);

        if (oper == '-')
            Console.Write("Answer is: " + int1 - int2);

        if(oper == '*')
            Console.Write("Answer is: " + int1 * int2);

        if(oper == '/')
            Console.Write("Answer is: " + int1 / int2);

        if(oper == '%')
            Console.Write("Answer is: " + int1 % int2);

        Console.ReadKey();
    }
}

}

5
  • 3
    try wrapping inside (): Console.Write("Answer is: " + (int1 - int2)); Commented Jan 27, 2014 at 10:13
  • 1
    That worked! Thank you. Why does the '-' operator require () unlike the others? Commented Jan 27, 2014 at 10:15
  • 1
    A word of advice, Use Switch/if..else..if in stead of so many if. Commented Jan 27, 2014 at 10:15
  • 1
    That would definitely clean it up a bit, thanks Commented Jan 27, 2014 at 10:17
  • 1
    It is not the operator. It is the context. Actually, you should enclose all your results in parenthesis if you wish to have the desired result. For example "Answer is: "+int1+int2 will return "Answer is: 32", if int1 is 3 and int2 is 2 Commented Jan 27, 2014 at 10:17

4 Answers 4

6
Console.Write("Answer is: " + int1 - int2);

You are first concatenating a string with int1, resulting in a string. Then you try to substract this string with int2.

Try this :

Console.Write("Answer is: " + (int1 - int2));

Thus you will first evaluate (int1 - int2) and then concatenate it.

By the way, you will also have to do it for the '+' operator. Else you will concatenate int2 to int1. For the other operators, it should work thanks to the precedence on '+'.

Sign up to request clarification or add additional context in comments.

2 Comments

... for very operator - not so. *, / and % have higher precedence than +. Whether he should do it for every operator is another matter :)
Thanks, I was not sure precedence would work the same with different types
3
Console.Write("Answer is: {0} ",int1 - int2);

Comments

1

Use a comma to separate parameters in Console.Write and use a placeholder {0}, like so:

Console.Write("Answer is: {0}", int1 - int2);

This does not confuse the compiler.

Comments

1
    if(oper == '+')
        Console.Write("Answer is: " + (int1 + int2));

    if (oper == '-')
        Console.Write("Answer is: " + (int1 - int2));

    if(oper == '*')
        Console.Write("Answer is: " + (int1 * int2);

    if(oper == '/')
        Console.Write("Answer is: " + (int1 / int2));

    if(oper == '%')
        Console.Write("Answer is: " + (int1 % int2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.