Question

I'm banging my head trying to figure out why I can't get my equations to not return as Zero as it seems that for some reason the MathOperations methods aren't doing there job? If anyone could help I would much appreciate it. Thanks in advance!

class MathUI
        {
            public void PromptForInt()
            {
                MathOperations ops = new MathOperations();
                Console.WriteLine("Enter first number to calculate");
                ops.Operand1 = int.Parse(Console.ReadLine());
                Console.WriteLine("\nEnter second number to calculate");
                ops.Operand2 = int.Parse(Console.ReadLine());
                return;
            }
            public void PromptForChoice()
            {
                int choice;

                MathOperations result = new MathOperations();
                Console.WriteLine("\nWhat type of operation would you like to perform?");
                Console.WriteLine("[1] Add \n[2] Subtract \n[3] Multiply \n[4] Divide \n[5] Exit \n");
                Console.Write("Enter your choice: ");
                choice = int.Parse(Console.ReadLine());

                if (choice == 1)
                {
                    Console.WriteLine(result.AddNumbers());
                }
                else if (choice == 2)
                {
                    Console.WriteLine(result.SubtractNumbers());
                }
                else if (choice == 3)
                {
                    Console.WriteLine(result.MultiplyNumbers());
                }
                else if (choice == 4)
                {
                    Console.WriteLine(result.DivideNumbers());
                }
                else if (choice == 5)
                {
                    Environment.Exit(0);
                }
                else
                {
                    Console.WriteLine("\nInvalid input entered!");
                }
            }

    class MathOperations
        {

            private int operand1;
            private int operand2;

            public int Operand1
            {
                get
                {
                    return operand1;
                }
                set
                {
                    operand1 = value;
                }
            }
            public int Operand2
            {
                get
                {
                    return operand2;
                }
                set
                {
                    operand2 = value;
                }
            }
            public MathOperations()
            {
                operand1 = 0;
                operand2 = 0;
            }
            public int AddNumbers()
            {
                return operand1 + operand2;
            }
            public int SubtractNumbers()
            {
                return operand1 - operand2;
            }
            public float DivideNumbers()
            {
                return (float)operand1 / (float)operand2;  // (float) used to show output with decimal point
            }
            public int MultiplyNumbers()
            {
                return operand1 * operand2;
            }
Was it helpful?

Solution

You are setting values for operand1 and operand2 for ops object but not result object.

MathOperations ops = new MathOperations();
Console.WriteLine("Enter first number to calculate");
ops.Operand1 = int.Parse(Console.ReadLine());
Console.WriteLine("\nEnter second number to calculate");
ops.Operand2 = int.Parse(Console.ReadLine());

but calling methods on result object whose operand1 and operand2 are initialized to 0 and hence the return values of functions. Remember ops' operand1 and operand2 hold user's input, not result's operand1 and operand2. You should use same ops object for result calculation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top