4 Replies - 577 Views - Last Post: 26 October 2015 - 09:32 AM Rate Topic: -----

#1 DecstarG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-October 15

Calculator Not Working? Wrong Answers.

Posted 24 October 2015 - 09:39 PM

I'm using the code:
            if (!operater_added)
            {
                sum += "1";
                textBox1.Text += "1";
            }
            else
            {
                sumP2 += "1";
                textBox1.Text += "1";
            }
        private void button11_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "2";
                textBox1.Text += "2";
            }
            else
            {
                sumP2 += "2";
                textBox1.Text += "2";
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "3";
                textBox1.Text += "3";
            }
            else
            {
                sumP2 += "3";
                textBox1.Text += "3";
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "4";
                textBox1.Text += "4";
            }
            else
            {
                sumP2 += "4";
                textBox1.Text += "4";
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "5";
                textBox1.Text += "5";
            }
            else
            {
                sumP2 += "5";
                textBox1.Text += "5";
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "6";
                textBox1.Text += "6";
            }
            else
            {
                sumP2 += "6";
                textBox1.Text += "6";
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "7";
                textBox1.Text += "7";
            }
            else
            {
                sumP2 += "7";
                textBox1.Text += "7";
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "8";
                textBox1.Text += "8";
            }
            else
            {
                sumP2 += "8";
                textBox1.Text += "8";
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "9";
                textBox1.Text += "9";
            }
            else
            {
                sumP2 += "9";
                textBox1.Text += "9";
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                textBox1.Text += "+";
                operater_added = true;
                operater = "+";
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                textBox1.Text += "-";
                operater_added = true;
                operater = "-";
            }
        }

        private void button32_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                textBox1.Text += "*";
                operater_added = true;
                operater = "*";
            }
        }

        private void button33_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                textBox1.Text += "/";
                operater_added = true;
                operater = "/";
            }
        }

        private void button34_Click(object sender, EventArgs e)
        {
            sum = "";
            textBox1.Text = "";
            operater_added = false;
            operater = "";
        }

        private void button35_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += ".";
                textBox1.Text += ".";
            }
            else
            {
                sumP2 += ".";
                textBox1.Text += ".";
            }
        }

        private void button36_Click(object sender, EventArgs e)
        {
            if (operater == "+") {
                int result = Convert.ToInt16(sum) + Convert.ToInt16(sumP2);
                textBox1.Text = result.ToString();
            }
            if (operater == "-")
            {
                int result = Convert.ToInt16(sum) - Convert.ToInt16(sumP2);
                textBox1.Text = result.ToString();
            }
            if (operater == "*")
            {
                int result = Convert.ToInt16(sum) * Convert.ToInt16(sumP2);
                textBox1.Text = result.ToString();
            }
            if (operater == "/")
            {
                int result = Convert.ToInt16(sum) / Convert.ToInt16(sumP2);
                textBox1.Text = result.ToString();
            }
        }

        private void button37_Click(object sender, EventArgs e)
        {
            if (!operater_added)
            {
                sum += "0";
                textBox1.Text += "0";
            }
            else
            {
                sumP2 += "0";
                textBox1.Text += "0";
            }
        }


And when I tried 7-5 I got a negative number. And I did 2+2 and got an error quit.
Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: Calculator Not Working? Wrong Answers.

#2 Peter O   User is offline

  • D.I.C Regular

Reputation: 128
  • View blog
  • Posts: 300
  • Joined: 19-October 13

Re: Calculator Not Working? Wrong Answers.

Posted 25 October 2015 - 02:43 AM

Shouldn't you reset the variables after you have calculated the result?

This post has been edited by Peter O: 25 October 2015 - 02:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6546
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Calculator Not Working? Wrong Answers.

Posted 25 October 2015 - 02:46 AM

Quote

And when I tried 7-5 I got a negative number. And I did 2+2 and got an error quit.

What negative number and what error?

You should take the time to rename all your controls to something more useful.

This post has been edited by andrewsw: 25 October 2015 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Calculator Not Working? Wrong Answers.

Posted 25 October 2015 - 06:12 AM

Four bits of advice:

1 - WinForms is legacy/end-of-life. You should move to WPF.
2 - See FAQ #35 below - "Q: I am building a calculator and..."
3 - See this tutorial for how to build a better keypad for your WinForms project if you are going to keep going this route.
4 - Take an hour and work the debugging tutorials linked below. That hour of learning a vital skill will save you hundreds of hours in your first semester alone.




tlhIn`toq's FAQ list - Updated Oct 2015


Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.
Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
Visualizing what your code does will let you see why it behaves the way it does.
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?


WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.


FAQ (Frequently Asked Questions - Updated Sep 2015
Spoiler



Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Calculator Not Working? Wrong Answers.

Posted 26 October 2015 - 09:32 AM

Just to add on to what others have been saying, it's literally impossible for us to diagnose your code's issues, because you have left the default name for every control. None of us know, for example, what button37 references. Hopefully it's a button, but that's all we know.

You desperately need to go in and rename every control with a contextually appropriate name. There are any number of valid standards. Personally I prefer names like firstNameTextBox and runCalculationButton, but anything is better than button49.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1