Skip to content

Commit 19ffd6e

Browse files
committed
Added error checking to the calculator
1 parent d5166aa commit 19ffd6e

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

calculatorGUI/calculatorGUI/Form1.Designer.cs

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

calculatorGUI/calculatorGUI/Form1.cs

+69-12
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,87 @@ public Form1()
1919

2020
private void plusButton_Click(object sender, EventArgs e)
2121
{
22-
double first = Convert.ToDouble(value1.Text);
23-
double second = Convert.ToDouble(value2.Text);
24-
resultBox.Text = Convert.ToString(first + second);
22+
23+
if (value1.Text == "" || value2.Text == "")
24+
{
25+
resultBox.Text = "Error: Please enter two valid values";
26+
}
27+
else
28+
{
29+
try
30+
{
31+
double first = Convert.ToDouble(value1.Text);
32+
double second = Convert.ToDouble(value2.Text);
33+
resultBox.Text = Convert.ToString(first + second);
34+
}
35+
catch(Exception ex)
36+
{
37+
resultBox.Text = "Error: Please input only numerical values";
38+
}
39+
}
2540
}
2641

2742
private void subractButton_Click(object sender, EventArgs e)
2843
{
29-
double first = Convert.ToDouble(value1.Text);
30-
double second = Convert.ToDouble(value2.Text);
31-
resultBox.Text = Convert.ToString(first - second);
44+
if (value1.Text == "" || value2.Text == "")
45+
{
46+
resultBox.Text = "Error: Please enter two valid values";
47+
}
48+
else
49+
{
50+
try
51+
{
52+
double first = Convert.ToDouble(value1.Text);
53+
double second = Convert.ToDouble(value2.Text);
54+
resultBox.Text = Convert.ToString(first - second);
55+
}
56+
catch (Exception ex)
57+
{
58+
resultBox.Text = "Error: Please input only numerical values";
59+
}
60+
}
3261
}
3362

3463
private void multiplyButton_Click(object sender, EventArgs e)
3564
{
36-
double first = Convert.ToDouble(value1.Text);
37-
double second = Convert.ToDouble(value2.Text);
38-
resultBox.Text = Convert.ToString(first * second);
65+
if (value1.Text == "" || value2.Text == "")
66+
{
67+
resultBox.Text = "Error: Please enter two valid values";
68+
}
69+
else
70+
{
71+
try
72+
{
73+
double first = Convert.ToDouble(value1.Text);
74+
double second = Convert.ToDouble(value2.Text);
75+
resultBox.Text = Convert.ToString(first * second);
76+
}
77+
catch (Exception ex)
78+
{
79+
resultBox.Text = "Error: Please input only numerical values";
80+
}
81+
}
3982
}
4083

4184
private void divideButton_Click(object sender, EventArgs e)
4285
{
43-
double first = Convert.ToDouble(value1.Text);
44-
double second = Convert.ToDouble(value2.Text);
45-
resultBox.Text = Convert.ToString(first / second);
86+
if (value1.Text == "" || value2.Text == "")
87+
{
88+
resultBox.Text = "Error: Please enter two valid values";
89+
}
90+
else
91+
{
92+
try
93+
{
94+
double first = Convert.ToDouble(value1.Text);
95+
double second = Convert.ToDouble(value2.Text);
96+
resultBox.Text = Convert.ToString(first / second);
97+
}
98+
catch (Exception ex)
99+
{
100+
resultBox.Text = "Error: Please input only numerical values";
101+
}
102+
}
46103
}
47104

48105
private void useResult1_Click(object sender, EventArgs e)

0 commit comments

Comments
 (0)