Grootpoot
Grootpoot

Reputation: 53

Calculator Trouble: Only getting second value I enter

I created a basic calculator, but each time I enter the second value and press the equal button, I get no result back. Only the value I entered 2nd. My methods for +-*/ are in a separate class. What will the best way be to resolve this problem? It is probably a simple error, but I can't find it. Can you also please give me an explanation on what I did wrong. Thanks in Advance.

public sealed partial class Calculator : Page
{
    public double num01, num02;
    int operater;

    public Calculator()
    {
        this.InitializeComponent();
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text =txtcalcdisplay.Text+ "1";          
    }

    private void btn2_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "2";            
    }

    private void btn3_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "3";
    }

    private void btn4_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "4";        
    }

    private void btn5_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "5";
    }

    private void btn6_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "6";
    }

    private void btn7_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "7";
    }

    private void btn8_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "8";
    }

    private void btn9_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = txtcalcdisplay.Text + "9";
    }

    private void Clear_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text = string.Empty;
    }

    private void btnsubtract_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(txtcalcdisplay.Text);
        txtcalcdisplay.Text = "";
        operater = '1';
    }

    private void btnadd_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(txtcalcdisplay.Text);
        txtcalcdisplay.Text = "";
        operater = '2';
    }

    private void btnmultiply_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(txtcalcdisplay.Text);
        txtcalcdisplay.Text = "";
        operater = '3';
    }

    private void btndivide_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(txtcalcdisplay.Text);
        txtcalcdisplay.Text = "";
        operater = '4';
    }

    private void btnequals_Click(object sender, RoutedEventArgs e)
    {
        switch (operater)
        {
            case 1:
                num02 = Convert.ToDouble(txtcalcdisplay.Text);
                CalculationClass sub = new CalculationClass();
                double answer= sub.Subtract(num01, num02);
                txtcalcdisplay.Text = answer.ToString();
                break;

            case 2:
                num02 = Convert.ToDouble(txtcalcdisplay.Text);
                CalculationClass add = new CalculationClass();
                answer= add.Addition(num01, num02);
                txtcalcdisplay.Text = answer.ToString();
                break;

            case 3:
                num02 = Convert.ToDouble(txtcalcdisplay.Text);
                CalculationClass mult = new CalculationClass();
                answer = mult.Multiply(num01, num02);
                txtcalcdisplay.Text = answer.ToString();
                break;

            case 4:
                num02 = Convert.ToDouble(txtcalcdisplay.Text);
                CalculationClass div = new CalculationClass();
                answer = div.Div(num01, num02);
                txtcalcdisplay.Text = Convert.ToString(answer);
                break;
        }
    }

    private void btnback_Click(object sender, RoutedEventArgs e)
    {
    }

    private void btnplusdivideminus_Click(object sender, RoutedEventArgs e)
    {
    }

    private void btncomma_Click(object sender, RoutedEventArgs e)
    {
    }

    private void btngallery_Click(object sender, RoutedEventArgs e)
    {
    }

    private void btncontact_Click(object sender, RoutedEventArgs e)
    {
    }

    private void num0_Click(object sender, RoutedEventArgs e)
    {
        txtcalcdisplay.Text += "0";
        num01 = Convert.ToDouble(txtcalcdisplay.Text);
    }
}


class CalculationClass
{
    double answer;

    public double Addition(double x, double y)
    {
        answer = x + y;
        return answer;
    }

    public double Subtract(double x, double y)
    {
        answer = x - y;
        return answer;
    }

    public double Multiply(double x, double y)
    {
        answer = x * y;
        return answer;
    }

    public double Div(double x, double y)
    {
        answer = x / y;
        return answer;
    }
}

Upvotes: 2

Views: 74

Answers (2)

Jamiec
Jamiec

Reputation: 136164

I'm not 100% certain, but I'm fairly sure this has to do with the fact that your variable operater is an int but you're assigning a character to it (this works - characters can be assigned to ints) and then comparing it back to the integer (eg doing 1 == '1')

int x = '1';
Console.WriteLine(x); // outputs 49
Console.WriteLine(x == 1); // outputs false

So to fix it either use the characters in your switch:

switch(operater){
   case '1': ...
}

Or assign the integer 1,2,3,4 rather than the character '1','2,'3','4'

private void btnadd_Click(object sender, RoutedEventArgs e)
{
    num01 = Convert.ToDouble(txtcalcdisplay.Text);
    txtcalcdisplay.Text = "";
    operater = 2; // here
}

Upvotes: 2

Becuzz
Becuzz

Reputation: 6857

So in your operator click handlers (the ones for +,-,*,/ etc.) you are setting your operater variable to a character '1','2', etc. Well, your operater variable is an int. But wait, you shouldn't be able to assign a char to an int?! Well, the compiler does an implicit conversion here (language spec says it should).

So what ends up happening when you do operater = '1' is that operater gets assigned 49 ('1''s ASCII value). Then when you get to your equals button click handler it hits that switch statement. And guess what? You don't have a case for 49. So nothing happens and you keep seeing your second number as the text on screen.

So to fix it, remove the single quotes around your numbers you assign to operater in your operator handlers. IE:

private void btnsubtract_Click(object sender, RoutedEventArgs e)
{
    num01 = Convert.ToDouble(txtcalcdisplay.Text);
    txtcalcdisplay.Text = "";
    operater = 1;  // <-- change this line to be like this, removed the single quotes
}

Upvotes: 1

Related Questions