RXPHXEL
I am stuck with variables
private void button1_Click(object sender, EventArgs e)
{
char op;
int first, second, result;
first = Convert.ToInt32(textBox1.Text); second = Convert.ToInt32(textBox2.Text); result = Convert.ToInt32(textBox4.Text); op = Convert.ToChar(textBox3.Text); switch (op) { case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': result = first / second; break; default: textBox3.Text = "Invalid Operator!"; break; } } I am trying to build a calculator with using the switches, my goal to take the user input from textbox 1 and 2 and by using the switches calculate the result. However, whenever I start the program it says "The input string was not correct" for the result line. I tried to convert it to int and double but it didn't work and I am simply stuck, how can I solve it?
first = Convert.ToInt32(textBox1.Text); second = Convert.ToInt32(textBox2.Text); result = Convert.ToInt32(textBox4.Text); op = Convert.ToChar(textBox3.Text); switch (op) { case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': result = first / second; break; default: textBox3.Text = "Invalid Operator!"; break; } } I am trying to build a calculator with using the switches, my goal to take the user input from textbox 1 and 2 and by using the switches calculate the result. However, whenever I start the program it says "The input string was not correct" for the result line. I tried to convert it to int and double but it didn't work and I am simply stuck, how can I solve it?
13 replies