-2

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;
            }

1 Answers1

3

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.

Muhammad Tauseef
  • 413
  • 2
  • 6
  • 18
  • I'm pretty new to this and I'm not completely sure how I would go about fixing it would you give me alittle more insight if possible? –  Mar 17 '14 at 04:51
  • In `promptForInt` you create ops object and take input in it's operands. In `promptForChoice` you create result object whose operand1 & operand2 are initialized to 0 as the constructor of `MathOperations` says. then you call function on result object which use result's operands which as already discussed hold 0. – Muhammad Tauseef Mar 17 '14 at 04:57
  • yep. nope that doesn't help just restated what you already stated. –  Mar 17 '14 at 04:59
  • In short, you should use same MathOperations object in both functions – Muhammad Tauseef Mar 17 '14 at 05:04
  • Ahh ok but how would I got about doing that then? –  Mar 17 '14 at 06:19