I am learning my first ever programming language - which is C#.
I am making my first project in my apprenticeship which is teaching me C#. It is to produce a basic calculator.
The basic calculator takes a string input and provides a result. For example, input: "5 + 5". The answer will be 10 in a decimal format.
However, part of my validation is make the even indexes of the string array only be numbers, whilst the odd indexes of the string array can only be operators of "+", "-", "*", "/". How would I do this?
I have tried to do it here but I am struggling:
for (int index = 0; index <= calculatorInput.Length; index++)
{
if (index % 2 == 0)
{
if (Decimal.TryParse(calculatorInput[index]))
{
throw new CalculatorException("Even indexes must contain a number");
}
//check for number
}
else if (//code here)
{
throw new CalculatorException("Odd indexes must contain an operator");
//check for operator
}
}
Sorry if this question is too simple, but I would greatly appreciate the help!