Home > front end >  C# Calculator Variables not reseting
C# Calculator Variables not reseting

Time:09-16

The calculartor will run and continue with the same calculation until you type stop then it will begin a new calculation but num1 will remain the last value it was in the previous calculation and not reset.

Example:

Enter a number 22

What calculation do you want to perform

Enter a number 33

Answer 55

What calculation do you want to perform /

Enter a number 5

Answer 11

What calculation do you want to perform stop

Enter a number 2

What calculation do you want to perform

Enter a number 9

Answer 20

      Boolean x;
        x = true;

        while (x == true)
        {
            double num1;
            double ans;
            double num2;
            string cal;
            Boolean y;
            y = true;

            Console.WriteLine("Enter a number ");
            num1 = Convert.ToInt32(Console.ReadLine());

            while (y == true)
            {

                Console.WriteLine("What calculation do you want to perform ");
                cal = Console.ReadLine();

                Console.WriteLine("Enter a number ");
                num2 = Convert.ToInt32(Console.ReadLine());

                switch (cal)
                {
                    case " ":
                        ans = add(num1, num2);
                        Console.WriteLine("Answer "   ans);
                        num1 = ans;
                        break;

                    case "-":
                        ans = sub(num1, num2);
                        Console.WriteLine("Answer "   ans);
                        num1 = ans;
                        break;

                    case "/":
                        ans = div(num1, num2);
                        Console.WriteLine("Answer "   ans);
                        num1 = ans;
                        break;

                    case "*":
                        ans = mul(num1, num2);
                        Console.WriteLine("Answer "   ans);
                        num1 = ans;
                        break;

                    case "Stop":
                        y = false;
                        break;
                }


            }
        }
    }

    private static double add(double firstnumber, double secondnumber)
    {
        double answer;
        answer = firstnumber   secondnumber;
        return answer;
    }
    private static double sub(double firstnumber, double secondnumber)
    {
        double answer;
        answer = firstnumber - secondnumber;
        return answer;
    }
    private static double div(double firstnumber, double secondnumber)
    {
        double answer;
        answer = firstnumber / secondnumber;
        return answer;
    }
    private static double mul(double firstnumber, double secondnumber)
    {
        double answer;
        answer = firstnumber * secondnumber;
        return answer;
    }

CodePudding user response:

just make it zero

 case "Stop":
 y = false;
  num1=0;
   break;

CodePudding user response:

The program expects "Stop" as input for cal in order to terminate, but you typed "stop". See the difference?

It is better to convert the input to lowercase with

cal = Console.ReadLine().ToLower();

and then check for it in the switch statement. Also always include a default branch in the switch because you need to expect the unexpected.

switch (cal)
{
    ..
    case "stop":
        y = false;
        break;
    default:
        // what happens if input is not  -*/ or stop?
        break;
}
  •  Tags:  
  • c#
  • Related