2

This is the wording of the exercise.

**Create a console application that asks the user for two numbers in the range 0-255 and then divides the first number by the second:

Enter a number between 0 and 255: 100

Enter another number between 0 and 255: 8

100 divided by 8 is 12

Write exception handlers to catch any thrown errors:

Enter a number between 0 and 255: apples

Enter another number between 0 and 255: bananas

FormatException: Input string was not in a correct format.**

This is the program I wrote, it works. But my intention was to make it shorter by writing byte result = divisor / dividend;

Both divisor and dividend were already casted as a byte, so why can't I use them directly in the code?

I had to calculate the operation using an int and then cast that int into a byte. (2 steps instead of one, which is puzzling me)

Am I missing something?

static void Main(string[] args)
    {
        checked
        {
            try
            {
                WriteLine("Enter a number between 0 and 255");
                string firstNumber = ReadLine();
                byte divisor = Convert.ToByte(firstNumber);
                WriteLine("Enter another between 0 and 255");
                string secondNumber = ReadLine();
                byte dividend = Convert.ToByte(secondNumber);
                int calculation = divisor / dividend;
                byte result = Convert.ToByte(calculation);
                WriteLine($"The result of the division between {divisor} and {dividend} is {result}");
            }
            catch (OverflowException)
            {
                WriteLine("The number you entered is either lower than 0 or superior to 255, please enter a number between 0 and 255");
            }
            catch (FormatException)
            {
                WriteLine("You didn't enter a number");
            }
            catch (Exception ex)
            {
                WriteLine($"{ex.GetType()} says {ex.Message}");
            }
        }

        ReadKey();
    }
4
  • 6
    As an aside - when dealing with whole numbers It's an obvious thing to think "I should use the smallest available numeric type that will hold my number" but your almost always better of using a plain old int: Why should I use int instead of a byte or short in C#
    – Alex K.
    Oct 9, 2018 at 16:13
  • 2
    Possible duplicate of byte + byte = int... why? - Realised there was a good dupe target
    – Equalsk
    Oct 9, 2018 at 16:16
  • 1
    Generally, 8 bit arithmetic is pretty useless except for bit-twiddling. So, it's not defined. Byte is an integral type, so byte arithmetic is define simply as integer arithmetic (as explained in the answers below).
    – Flydog57
    Oct 9, 2018 at 16:34
  • 2
    By the way, I somehow expect that your teacher/professor wanted you to figure this out during your assignment. Feel free to tell him/her that at least one Stack Overflow contributor thinks you asked a great "homework" question: you explained your problem clearly and you showed your code, identifying where your issue was. Your question is so much better than the typical homework question (which mostly reduces to "My professor asked me to do this - can you do it for me).
    – Flydog57
    Oct 9, 2018 at 16:38

2 Answers 2

2

It's because byte doesn't define any arithmetic operators.

The code for divisor / dividend is the equivalent of (int)divisor / (int)dividend which is why you get an error if you try and store the result in a byte.

What you're doing is correct, or alternatively write byte calculation = (byte)(divisor / dividend);

0
1

You can cast it:

byte calculation = (byte)(divisor / dividend);

Refer this link for more details: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/byte

1
  • Thanks! As I said before, it worked like a charm. You guys are awesome. Best regards
    – laxcivo
    Oct 9, 2018 at 18:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.