1
\$\begingroup\$

One of my friends was creating an app with following requirements and having been someone with slightly more coding experience, I wanted to help:

Create a calculator that does one arithmetic operation at a time and prints the result to the screen.

  • Prompt the user for a number.
  • Prompt the user for an operation (+ - / *).
  • Prompt the user for another number.
  • Perform the operation.
  • Print the result to the screen.
  • Repeat until the user types in “quit” at any of the prompts.
using System;

namespace Calc
{
    class Program
    {
        static void Main(string[] args)
        {
            // We are creating the strings and integers we need beforehand.
            // These will be updated according to the user input.
            string firstInput = "";
            string secondInput = "";
            string operand = "";
            double x = 0;
            double y = 0;

            while (true)
            {
                Console.WriteLine("You can quit any time by typing \"quit\".");

                Console.WriteLine("Please enter the first number:");
                First:
                firstInput = Console.ReadLine();
                try
                {
                    if (firstInput.ToLower() != "quit")
                    {
                        x = double.Parse(firstInput);
                    }
                    else
                    {
                        break;
                    }
                }
                catch
                {
                    // If we are here, that means erroneous input was given.
                    Console.WriteLine("Invalid input. Please enter an integer.");
                    goto First;
                }

                Console.WriteLine("Please enter an operand (+, -, *, /):");
                Second:
                operand = Console.ReadLine();
                //We need to make sure user has entered a proper operator.
                if (operand.ToLower() == "quit")
                {
                    break;
                }
                else if (!((operand == "+") || (operand == "-") || (operand == "*") || (operand == "/")))
                {
                    Console.WriteLine("Invalid input. Please enter one of the following: +, - , *, /.");
                    goto Second;
                }
                else
                {
                    // No problem. Keep going.
                }

                Console.WriteLine("Please enter the second number:");
                Third:
                secondInput = Console.ReadLine();
                try
                {
                    if (secondInput.ToLower() != "quit")
                    {
                        y = double.Parse(secondInput);
                    }
                    else
                    {
                        break;
                    }
                }
                catch
                {
                    // If we are here, that means erroneous input was given.
                    Console.WriteLine("Invalid input. Please enter an integer.");
                    goto Third;
                }

                // We have all we need if program has run up until this line.
                // We can now check which operand is entered to determine the result.
                if (operand == "+")
                {
                    Console.WriteLine($"{firstInput} + {secondInput} = {x + y}");
                }
                else if (operand == "-")
                {
                    Console.WriteLine($"{firstInput} - {secondInput} = {x - y}");
                }
                else if (operand == "*")
                {
                    Console.WriteLine($"{firstInput} * {secondInput} = {x * y}");
                }
                else if (operand == "/")
                {
                    Console.WriteLine($"{firstInput} / {secondInput} = {x / y}");
                }
                else
                {
                    Console.WriteLine("Error! You must have entered invalid input.");
                }
                Console.WriteLine("Press any key to throw me away like a Kleenex.");
                Console.ReadKey();
                break;
            }

        }
    }
}

Couple things I've attempted/found out so far:

  • Instead of checking whether "quit" has been entered every time, I tried to use following with no success: while (!((firstInput.ToLower() == "quit") || (secondInput.ToLower() == "quit") || (operand.ToLower() == "quit")))

  • Apparently C# has "String Interpolation" too, like in JS (I guess with ES6) which was considered as a good practice among my colleagues if nothing else, for readability.

  • I know usage of "goto" is not considered well usually. Though it seemed quite practical with the labels. Priorly, I had thought it was only used in switch cases.

  • About exception handling, I haven't had much use before when I messed around in Java, but I suppose it's one of the most fundamental features of higher level languages to keep program more prune to run-time errors. This one particularly came to my head when I realized user could potentially enter some input that can't be parsed properly.

  • I don't know, there's not much to do given that problem and implementation is quite simple, but I still think it's a good opportunity to develop good practices if I were to somewhat get involved with gaming development, for example (Unity etc.).

I've been recently coding Haskell, Python, JavaScript and I have C/C++ background but this is the first time I laid my hands on an .NET/C# app. I have tinkered with Java before, but I don't really have much experience with higher level languages. Anyways, long story short, I'd like to see what could have been done better and what are the things that are considered as good/bad practice that could be particularly viewed in this small code snippet.

P.S: I'm a bit aware that comments are a bit overly-done, but it's intentional for obvious reasons. Thanks in advance.

\$\endgroup\$
  • \$\begingroup\$ I would use "TryParse" instead of Parse, maybe even int.TryParse (you are looking for an integer) \$\endgroup\$ – D Ie Apr 2 at 12:32
  • \$\begingroup\$ There is a very similar question that might interest you: codereview.stackexchange.com/questions/217792/… \$\endgroup\$ – dfhwze May 18 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.