I recently created an intermediate calculator program, primarily to practice programming in C# but also for use with my college subjects. Any feedback on coding practices I can implement to optimise this program and any other programs I write in the future would be greatly appreciated.
Thanks.
using System;
namespace calculatormessingaroundthingy
{
class Program
{
static void Main()
{
Console.WriteLine("Hello World!");
bool loopInt=false; // Sets value of loop variable to false.
while (loopInt == false) // Causes the program to repeatedly run until the user chooses to stop.
{
MessageOptions(); // Calls a procedure which lists the user's options.
var input = Console.ReadLine();
int inputInt;
while ((!int.TryParse(input, out inputInt)) | (!(inputInt>=0 && inputInt<=6))) // Loop repeats while either the user's input can't be passed into an int variable or while the int is not between 0 and 6 inclusive.
{
Console.WriteLine("ERROR: Invalid Input");
MessageOptions();
input = Console.ReadLine();
}
if (inputInt==0) // Input of 0 exits the program
{
Console.WriteLine("Goodbye!");
loopInt = true;
break;
}
FirstInput(inputInt); // Calls a procedure which gets the user's first number, the message depending on the user's previous input.
var strNum1 = Console.ReadLine();
double num1;
while ((!double.TryParse(strNum1, out num1))) // Loop repeats while the user's input can't be passed into a double variable.
{
Console.WriteLine("ERROR: Invalid Input");
FirstInput(inputInt);
strNum1 = Console.ReadLine();
}
SecondInput(inputInt); // Calls a procedure which gets the user's first number, the message depending on the user's previous input
var strNum2 = Console.ReadLine();
double num2;
while ((!double.TryParse(strNum2, out num2))) // Loop repeats while the user's input can't be passed into a double variable.
{
Console.WriteLine("ERROR: Invalid Input");
SecondInput(inputInt);
strNum2 = Console.ReadLine();
}
switch (inputInt) // Passes the user's two numbers into corresponding procedure for a certain mathematical operation.
{
// inputInt corresponds to the user's respones to the operation they wish to perform.
case 1:
Console.WriteLine(Add(num1, num2));
break;
case 2:
Console.WriteLine(Subtract(num1, num2));
break;
case 3:
Console.WriteLine(Multiply(num1, num2));
break;
case 4:
Console.WriteLine(Divide(num1, num2));
break;
case 5:
Console.WriteLine(Powers(num1, num2));
break;
case 6:
Console.WriteLine(Logarithm(num1, num2));
break;
}
}
}
static double Powers(double number, double power) // Raises the first number to the power of the second number and returns the result.
{
return Math.Pow(number, power);
}
static double Add(double number, double number2) // Adds together both numbers and returns the result.
{
return number + number2;
}
static double Subtract(double number, double number2) // Subtracts the second number from the first number and returns the result.
{
return number - number2;
}
static double Multiply(double number, double number2) // Multiplies together both numbers and returns the result.
{
return number * number2;
}
static double Divide(double number, double number2) // Divides the first number by the second number and returns the result.
{
return number / number2;
}
static double Logarithm(double number, double number2) // Returns the logarithm of base first number and argument second number.
{
return Math.Log(number2, number);
}
static public void MessageOptions() // Displays the user's inital options.
{
Console.WriteLine();
Console.WriteLine("-------------------------------------");
Console.WriteLine("Choose one of the following options: ");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("5. Powers");
Console.WriteLine("6. Logarithms");
Console.WriteLine("0. Exit");
Console.WriteLine("-------------------------------------");
}
static public void FirstInput(int input) // Displays what number should be entered dependent on the inital input.
{
switch (input)
{
case 1: case 2: case 3: case 4:
Console.WriteLine("Enter the first number: ");
break;
case 5:
Console.WriteLine("Enter the base number: ");
break;
case 6:
Console.WriteLine("Enter the logarithm's base: ");
break;
}
}
static public void SecondInput(int input) // Displays what number should be entered dependenent on the inital input.
{
switch (input)
{
case 1: case 2: case 3: case 4:
Console.WriteLine("Enter the second number: ");
break;
case 5:
Console.WriteLine("Enter the exponent: ");
break;
case 6:
Console.WriteLine("Enter the logarithm's argument: ");
break;
}
}
}
}
```
calculatormessingaroundthingy
- Naming is hard. I don't blame you. \$\endgroup\$