0

I am trying to use my three variables (num1, num2 and oper) in each of my methods but I can't find a way to do this anywhere in my book or on the internet. I realize there is much to be done before my program is ready and I will get it cleaned up as I learn. Right now I just need to know the code for importing the variables to different methods, I don't need the code fixed or improved. I hope this makes sense, I'm very new to this so forgive my ignorance and bear with me. Thank you very much for reading!

public class SimpleCalc
{
    public double SimpleCalc(double num1, double num2, string oper)
    {
        Console.Write("Enter first integer: ");
        num1 = Convert.ToDouble(Console.ReadLine());

        Console.Write("Enter operator (+,-,*, / or %)");
        oper = Convert.ToString(Console.ReadLine());

        Console.Write("Enter second integer: ");
        num2 = Convert.ToDouble(Console.ReadLine());

        if (oper == "+");
        return addNumbers();

        if (oper == "-");
        return subtractNumbers();

        if (oper == "*");
        return multiplyNumbers;

        if (oper == "/");
        return divideNumbers;
    }

    public double addNumbers()
    {
        Console.Write("The answer is: ", num1 + num2);
    }

    public double subtractNumbers()
    {
        Console.Write("The answer is: ", num1 - num2);
    }

    public double multiplyNumbers()
    {
        Console.Write("The answer is: ", num1 * num2);
    }

    public double divideNumbers()
    {
        Console.Write("The answer is: ", num1 / num2);
    }
}
4
  • 1
    Your ifs don't do anything; you should remove the ; after them, otherwise it just means if (condition) then (empty instruction) Commented Feb 4, 2014 at 23:20
  • Nothing in your previous questions on the same subject has been of help? Commented Feb 4, 2014 at 23:21
  • @Steve I haven't asked a question on this topic, I had a problem a few days ago with the parameters but that was a completely different situation. Commented Feb 4, 2014 at 23:57
  • The division should cater for situations when num2 is zero. Commented Feb 5, 2014 at 0:50

1 Answer 1

2

The easiest way it to simply declare them as parameters:

public double addNumbers(double num1, double num2)
{
    return num1 + num2;
}

public double subtractNumbers(double num1, double num2)
{
    return num1 - num2;
}

public double multiplyNumbers(double num1, double num2)
{
    return num1 * num2;
}

public double divideNumbers(double num1, double num2)
{
    return num1 / num2;
}

And pass the parameters to the other functions like this:

if (oper == "+")
    return addNumbers(num1, num2);

if (oper == "-")
    return subtractNumbers(num1, num2);

if (oper == "*")
    return multiplyNumbers(num1, num2);

if (oper == "/")
    return divideNumbers(num1, num2);

Note that I've modified your methods because they need to return a value. I've also modified the if statements because you had a semi-colon after each condition, which actually results in an empty conditional, followed by an immediate, unconditional return.

However, if you really need to store state variables within your class and reuse them in multiple methods, you should use fields or properties (which are usually backed by fields).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.