Click here to Skip to main content
15,444,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Alright so, I'm a very new coder. As in I just started learning yesterday. I've probably spent about 2 hours actually learning. I tried to make a very simple calculator and it was going great until I got to the very last line of the code. I was trying to make it subtract two numbers but it just wouldn't work. It was working just fine before I added in the text before the answer but as soon as I would put in the text ("Your answer is ") it would start giving this error : Operator '-' cannot be applied to operands of type 'string' and 'int'. I would understand this error except for the fact that they're both integers and neither of them are strings. Here's the actual code:

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

namespace calc
{
    class Program
    {
        static void Main(string[] args)
        {
        Start:
            Console.WriteLine("Enter a number");
            string input = Console.ReadLine();
            int number;
            bool isNumber = Int32.TryParse(input, out number);
            if (isNumber == true)
            {
                Console.WriteLine("The number you have entered is " + number);
            }
            else
            {
                Console.WriteLine("you did not enter a number");
                goto Start;
            }
            Console.WriteLine("* or + or - or /");
            string calculation = Console.ReadLine();
            if (calculation == "*")
            {
                Console.WriteLine("Which number do you want to multiply with");
                string multiply = Console.ReadLine();
                int multiplication;
                bool canMultiply = Int32.TryParse(multiply, out multiplication);
                Console.WriteLine("Your answer is " + number * multiplication);
                goto Start;
            }
            else if (calculation == "/")
            {
            beginningofdivision:
                Console.WriteLine("Which number do you want to divide with");
                string divide = Console.ReadLine();
                int division;
                bool canDivide = Int32.TryParse(divide, out division);
                Console.WriteLine("Your answer is " + number / division);
                if (divide == "0")
                {
                    Console.WriteLine("Cannot divide with 0");
                    goto beginningofdivision;
                }
                goto Start;
            }
            else if (calculation == "+")
            {
                Console.WriteLine("Which number do you want to add");
                string add = Console.ReadLine();
                int addition;
                bool canAdd = Int32.TryParse(add, out addition);
                Console.WriteLine("Your answer is " + number + addition);
                goto Start;
            }
            else
            {
                Console.WriteLine("Which number do you want to subtract");
                string subtract = Console.ReadLine();
                int subtraction;
                bool canSubtract = Int32.TryParse(subtract, out subtraction);
                Console.WriteLine("Your answer is " + number - subtraction);
                goto Start;
            }
        }
    }
}


What I have tried:

I've tried turning the "Your answer is " part into a string. I've tried concatenating the space and the actual text separately like so, ("Your answer is" + " " + number - subtraction) but that didn't work either. I don't know what else to do.
Posted
Updated 24-Aug-21 5:41am
Comments
Gerry Schmitz 24-Aug-21 10:41am     CRLF
Console.WriteLine( $"Your answer is {number - subtraction}" ); Mixing string and numbers involves different techniques.

The reason is operator precedence. In other words the calculation for + and - is done from left to right meaning that number is first added (concatenated) to the string and then you try to subtract another number from the concatenated string.

Try adding parenthesis and you'll notice the difference:
C#
Console.WriteLine("Your answer is " + (number - subtraction));


For more information, have a look at C# operators and expressions - C# reference | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
BillWoodruff 24-Aug-21 20:25pm    
+5
To add to what Wendelus has said, remove all the goto statements and all the labels - replace them with loops like while:
while (true)
    {
    Console.WriteLine("Which number do you want to divide by?");
    ​int division;
    if (int32.TryParse(Console.ReadLine(), out division))
        {
        if (division == 0)
            {
            Console.WriteLine("Cannot divide by 0");
            }
        else
            {
            Console.WriteLine("$Your answer is {number / division}");
            break;
            }
        }
    }
Your code becomes much more readable, and it's a lot harder to generate "spaghetti code" that is hard to understand and maintain.

Then forget goto even exists for five years or so - by then you'll understand when it is a good idea to use it! (And I've been coding in C# for 15 years or more, and I've never had to use one!)
 
Share this answer
 
Comments
BillWoodruff 24-Aug-21 20:25pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900