This project is a basic calculator program written in C#. It allows users to perform basic mathematical operations such as addition,
subtraction, multiplication, division, modulo, and exponentiation. The program prompts the user to input two numbers and a mathematical operator and then displays the result of the operation.
It includes robust input validation to ensure the entered values are valid numbers and that the operator used is one of the allowed operations.
Key Features
- Input Validation: Ensures that the user enters valid numbers and mathematical operators.
- Supported Operations: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulo (%), and Exponentiation (^).
- Handling Division by Zero: Safeguards against division by zero, notifying the user if they attempt this operation.
- Option to Quit: After each calculation, the user can either quit the program or continue by inputting new values.
Problems Solved During Development
- Input Validation:
- One of the key challenges in this project was ensuring that the user input was correctly handled.
Initially, the program could crash if invalid data was entered (such as letters or special characters when numbers were expected)
- Solution: Implemented a method to ensure only valid numbers are accepted, using double.TryParse(). This prevents the program from crashing when invalid inputs are entered and instead prompts the user to try again.
- Handling Invalid Mathematical Operators:
- Early versions allowed the user to input any string, which would cause errors if the operator was not recognized.
- Solution: Added a validation mechanism that checks whether the entered operator is part of a predefined set of allowed operators (+, -, *, /, %, ^). If the input is invalid, the user is prompted to re-enter a valid operator.
- Division by Zero:
- Division by zero is a common issue that needs to be handled gracefully in mathematical calculations. Without proper checks, this can lead to runtime exceptions.
- Solution: Included a specific check in the code to detect division by zero. If detected, a custom message informs the user that division by zero is not allowed.
- Exponentiation Edge Case:
- Exponentiation of any number raised to the power of zero should always return 1, but ensuring this is calculated correctly required an additional edge case check.
- Solution: Implemented a condition to handle cases where the exponent is zero and return 1 directly without performing unnecessary calculations.
- Program Flow Control:
- Allowing the user to quit or continue based on their input posed a challenge in managing program flow.
- Solution: Added a simple mechanism where the user can type q to quit the program, or press Enter to continue, maintaining a user-friendly interface.
This calculator project serves as a solid foundation for further development, such as adding additional mathematical functions, support for more complex expressions, or improving the user interface.