I'm making a calculator that performs operations sequentially (from left to right, not precedence), using ASP.NET/C#, but I'm having trouble trying to get the loops that run through the input box in my code behind to work right.
Obviously to me the logic seems to make sense, so I can't figure out why I get a runtime error if I enter just a number and no operands, and while my error checking does work when it comes to ensuring there are no two operators together, it displays that error even when I have two or more sets of numbers and operands placed correctly between them (I.E 24+5).
I am aware its a lot of code, and while I tried to put in as many comments to explain what's going on, I believe the problem lies within my loop and how I want it to build a string until it reaches an operator or the end of the input string.
Obviously to me the logic seems to make sense, so I can't figure out why I get a runtime error if I enter just a number and no operands, and while my error checking does work when it comes to ensuring there are no two operators together, it displays that error even when I have two or more sets of numbers and operands placed correctly between them (I.E 24+5).
I am aware its a lot of code, and while I tried to put in as many comments to explain what's going on, I believe the problem lies within my loop and how I want it to build a string until it reaches an operator or the end of the input string.
protected void btnEquals_Click(object sender, EventArgs e)
{
{
lblError.Text = "";
if (txtResult.Text.Length > 0)
{
double result = 0;
Calculator myCalculator = new Calculator();
string input = txtResult.Text;
string Oper = "";
int j = 0;
while (j < input.Length)
{
string temp = "";
double num1 = 0;
double num2 = 0;
/* get the numbers from the input string in order until the end of string or next operator */
while (input.Substring(j, 1) != "+" && input.Substring(j, 1) != "-" && input.Substring(j, 1) != "*" && input.Substring(j, 1) != "/")
{
temp += input.Substring(j, 1);
j++;
}
num1 = Convert.ToDouble(temp); /*create number using the built substring */
/*check if next part of input is an operator, and if not check if end of string*/
if (input.Substring(j, 1) == "+" || input.Substring(j, 1) == "-" || input.Substring(j, 1) == "*" || input.Substring(j, 1) == "/")
{
Oper = input.Substring(j, 1);
}
else if (input.Substring(j, 1) == "")
{
if (result != 0)
{
switch (Oper)
{
case "+":
result = myCalculator.Add(num1, num2);
break;
case "-":
result = myCalculator.Subtract(num1, num2);
break;
case "*":
result = myCalculator.Multiply(num1, num2);
break;
case "/":
result = myCalculator.Divide(num1, num2);
break;
}
}
txtResult.Text = result.ToString();
break;
}
/*check if next part of input is another operator, or end of string, and display error if true*/
if (input.Substring(j, 1) == "+" || input.Substring(j, 1) == "-" || input.Substring(j, 1) == "*" || input.Substring(j, 1) == "/" && input.Substring(j, 1) != "")
{
lblError.Text = "You can't have two operands next to each other or an operand without a number to its right";
break;
}
/* get the numbers from the input string in order until the end of string or next operator (to build second number) */
temp = "";
while (input.Substring(j, 1) != "+" && input.Substring(j, 1) != "-" && input.Substring(j, 1) != "*" && input.Substring(j, 1) != "/")
{
temp += input.Substring(j, 1);
j++;
}
num2 = Convert.ToDouble(temp); /*create number using the built substring*/
/*perform operation using calculator class*/
switch (Oper)
{
case "+":
result = myCalculator.Add(num1, num2);
break;
case "-":
result = myCalculator.Subtract(num1, num2);
break;
case "*":
result = myCalculator.Multiply(num1, num2);
break;
case "/":
result = myCalculator.Divide(num1, num2);
break;
}
/*if an operand still exists, check if there is another operand or null after that and print error, otherwise, repeat loop*/
if (input.Substring(j, 1) == "+" || input.Substring(j, 1) == "-" || input.Substring(j, 1) == "*" || input.Substring(j, 1) == "/")
{
Oper = input.Substring(j, 1);
j++;
}
else if (input.Substring(j, 1) == "")
{
break;
}
/*check if next part of input is another operator, or end of string, and display error if true*/
if (input.Substring(j, 1) == "+" || input.Substring(j, 1) == "-" || input.Substring(j, 1) == "*" || input.Substring(j, 1) == "/" && input.Substring(j, 1) != "")
{
lblError.Text = "You can't have two operands next to each other or an operand without a number to its right";
break;
}
} /*repeat loop by reading next number (WIP)*/
txtResult.Text = result.ToString();
}
else
{
txtResult.Text = string.Empty;
lblError.Text = "There was no input entered!";
}
}
}