'Continue to calculate with the outcome
I am creating a calculator in c#. Something's not working for me. A standard calculation works for me. For example 13+15. The result is then 28. But if I want to continue calculating with 28, it goes wrong. For example 28 (the result of the previous sum) + 3. You would say it is 31, but my calculator comes out at 21. This is with every math problem, if I want to calculate further with it. How can I make sure it's correct? Do I need a loop? I do want to be able to continue calculating with the result 31. Does anyone have an idea how I can take care of this?
Btw: I have asked this question earlier, but the answers were all wrong:(.
This is my code (feel free to put it in there ;))
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
string first = "";
string second = "";
string userInput = "";
string space = " ";
char function;
int times;
double result;
public Form1()
{
InitializeComponent();
}
private void number1_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "1";
calculatorDisplay.Text += userInput;
}
private void number2_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "2";
calculatorDisplay.Text += userInput;
}
private void number3_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "3";
calculatorDisplay.Text += userInput;
}
private void number4_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "4";
calculatorDisplay.Text += userInput;
}
private void number5_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "5";
calculatorDisplay.Text += userInput;
}
private void number6_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "6";
calculatorDisplay.Text += userInput;
}
private void number7_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "7";
calculatorDisplay.Text += userInput;
}
private void number8_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "8";
calculatorDisplay.Text += userInput;
}
private void number9_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "9";
calculatorDisplay.Text += userInput;
}
private void clearButton_Click(object sender, EventArgs e)
{
first = "";
second = "";
userInput = "";
result = 0.0;
calculatorDisplay.Text = "0";
feedback.Text = "";
times = 0;
}
private void divideButton_Click(object sender, EventArgs e)
{
function = '/';
first = userInput;
userInput = "";
feedback.Text = first + space + "÷";
}
private void multiplyButton_Click(object sender, EventArgs e)
{
function = '*';
first = userInput;
userInput = "";
feedback.Text = first + space + "*";
}
private void plusButton_Click(object sender, EventArgs e)
{
function = '+';
first = userInput;
userInput = "";
feedback.Text = first + space + "+";
}
private void minusButton_Click(object sender, EventArgs e)
{
function = '-';
first = userInput;
userInput = "";
feedback.Text = first + space + "-";
}
private void equalButton_Click(object sender, EventArgs e)
{
times++;
second = userInput;
double firstNum, secondNum;
firstNum = Convert.ToDouble(first);
secondNum = Convert.ToDouble(second);
if (function =='+')
{
result = firstNum + (secondNum * times);
calculatorDisplay.Text = result.ToString();
feedback.Text = (result - secondNum) + space + "+" + space + second + space + "=";
}
else if(function == '-')
{
result = firstNum - (secondNum * times);
calculatorDisplay.Text = result.ToString();
feedback.Text = (result+secondNum) + space + "-" + space + second + space + "=";
}
else if (function == '/')
{
if(secondNum == 0)
{
calculatorDisplay.Text = "Error";
}
else
{
result = firstNum / Math.Pow(secondNum, times);
calculatorDisplay.Text = result.ToString();
feedback.Text = result*secondNum + space + "÷" + space + second + space + "=";
}
}
else if (function == '*')
{
result = firstNum * Math.Pow(secondNum, times);
calculatorDisplay.Text = result.ToString();
feedback.Text = result/secondNum + space + "*" + space + second + space + "=";
}
}
private void decimalButton_Click(object sender, EventArgs e)
{
calculatorDisplay.Text += ".";
}
private void zeroButton_Click(object sender, EventArgs e)
{
calculatorDisplay.Text = "";
userInput += "0";
calculatorDisplay.Text += userInput;
}
}
}
Solution 1:[1]
As has been commented on the original post, learning to use the debugger is key here.
Following the state of first, second and userInput, this is what I see (manual debugging for the sake of trying to guide you a little bit):
After having performed the first calculation (13 + 15 =), this is what you will have:
first = "13"
second = "15"
userInput = "15"
(None of these values are reset at the end of equalButton_Click(), which may be expected?)
Then, you click the + button, and your state is updated to:
first = "15" // userInput value
second = "15"
userInput = "" // reset
Then, you click the 3 button, and your state is updated to:
first = "15"
second = "15"
userInput = "3"
Then, you click the = button, which updates the state to:
first = "15"
second = "3"
userInput = "3"
Seeing as you are using your times counter, which is now equal to 2 (after having clicked the = button twice in total), your calculation will therefore be 15 + (3 * 2) => 15 + 6 => 21.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Astrid E. |
