2
\$\begingroup\$

So I made this calculator in c# console, I'm pretty nooby-ish in programming. I want to know if I can make it better, because my coding skills suck. Pretty sure my logic sucks too.

Any advice and tips?

   public static void Main() {
        bool returnToStart;

        do {
            returnToStart = Calc();

        } while(returnToStart == true);

        Console.ReadLine();

    }
    public static bool Calc() {
        ExitProgram();
        Console.Clear();

        float firstNumber; float secondNumber; float result; 
        string operation;

        Console.Write("First number: ");
        firstNumber = float.Parse(Console.ReadLine());

        Console.Write("Choose your operator: addition(+), subtraction(-), " +
                     "multiplication(*) or division(/): ");
        operation = Console.ReadLine();

        Console.Write("Second number: ");
        secondNumber = float.Parse(Console.ReadLine());

        Console.Clear();


        switch(operation) {
            case "+":
                result = (firstNumber + secondNumber);
                Console.WriteLine("Calculation: {0}", result);
                break;
            case "-":
                result = (firstNumber - secondNumber);
                Console.WriteLine("Calculation: {0}", result);
                break;
            case "*":
                result = (firstNumber * secondNumber);
                Console.WriteLine("Calculation: {0}", result);
                break;
            case "/":
                if(firstNumber == 0) {
                    Console.WriteLine("Cant divide by zero!");
                } else {
                    result = (firstNumber / secondNumber);
                    Console.WriteLine(result);
                }
                break;

            default:
                Console.WriteLine("Error! Try again.");
                break;


        }
        return true;

    }
    public static bool ExitProgram() {
        string exit = "Exit";

        Console.WriteLine("If you're done with your calculations,\nthen you can exit this program by simply typing in 'Exit' ");
        Console.Write("Want to quit the program?: ");
        exit = Console.ReadLine();

        if(exit == "Exit") {
           Environment.Exit(0);

        }return true;

    }

}

}

\$\endgroup\$
5
\$\begingroup\$

Keep It Simple For the User Rather than making the user type the word Exit it might be better to ask the user Are you done yet(y/n)? and accept a simple yes/no answer. It also might be better if the answer was not case sensitive so accept Y, y, N and n.

Exit From Main() When Possible The Main() function may contain clean up code so exiting from the ExitProgram() function may not be the best idea. A second problem I see with the ExitProgram() function is that it is called before the user ever enters a calculation. There are two possible ways to handle this, one would be to move the call to ExitProgram() to after the calculation is performed, the other would be to have a function that contains a do while loop that calls Calc within the loop and tests ExitProgram() in the while condition.

Function Complexity
The function Calc() is overly complex (it does too much) and should be multiple functions. One of the functions should get the user input, and a second function should do the calculation. This would be applying two programming principles, the Single Responsibility Principle and the KISS Principle.

The Single Responsibility Principle states:

that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by that module, class or function.

The Keep It Simple (KISS) Principle is an engineering principle that predates computers, basically it is keep the implementation as simple as possible.

\$\endgroup\$

Your Answer

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

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