Search code examples
c#variablesswitch-statementlocal

Local variables - switch case


I am studying programming and I am very beginner (started 2 months ago). I am doing a c# exercise for maths calculation. Our professor used a if ... else (embricated) loops to do the exercise. I wanted to use a switch case but I am struggling with the local variables.

I understand the issue: case 2 and 3 does not "know" the variables totalNumber and nbrSubAssy is as they come from case 1 and case 2, then they are detected as not assigned.

If I still want to use a switch case, what could I do to solve it?

using System;

namespace Denombrements
{
    class Program
    {

        static long IntMultiplication(int startValue, int endValue)
        {
            long multiplication = 1;
            for (int k = startValue; k <= endValue; k++)
                multiplication *= k;
            return multiplication;
        }

        static int UserSelection(String message)
            {
                int number = 0;
                Console.Write(message);
                try
                {
                    number = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                }

                catch
                {
                    Console.WriteLine();
                    Console.WriteLine("Wrong input, enter an integer");
                }

                return number;

            }

        static void Main(string[] args)
        {
            char choice = '1';
            while (choice != '0')
            {
                Console.WriteLine("Permutation ...................... 1");
                Console.WriteLine("Arrangement ...................... 2");
                Console.WriteLine("Combination ...................... 3");
                Console.WriteLine("Quit ............................. 0");
                Console.Write("Choice :                           ");
                choice = Console.ReadKey().KeyChar;
                Console.WriteLine();

                switch(choice)
                {  
                    case '0':
                        Environment.Exit(0);
                        break;
                    case '1':
                        int totalNumber = UserSelection("Total number of elements to be taken into account");
                        long permutation = IntMultiplication(1, totalNumber);
                        Console.WriteLine(totalNumber + "! = " + permutation);
                        break;
                    case '2':
                        int nbrSubAssy = UserSelection("Total number of elements in the subassy to be taken into account");
                        long arrangement = IntMultiplication(totalNumber - nbrSubAssy + 1, totalNumber);
                        Console.WriteLine("A(" + totalNumber + "/" + nbrSubAssy + ") = " + arrangement);
                        break;
                    case '3':
                        long combination = arrangement / IntMultiplication(1, nbrSubAssy);
                        Console.WriteLine("C(" + totalNumber + "/" + nbrSubAssy + ") = " + combination);
                        break;
                    default:
                        Console.WriteLine("Wrong input");
                        break;
                }
            }
            Console.ReadLine();
        }
    }
}

Solution

  • Declare your variable before While loop and it will keep the value of it for all the life of LOOP and you could access the old values

    char choice = '1';
    int nbrSubAssy = 0;
    int totalNumber = 0;
    long arrangement = 0;
    
    while (choice != '0')
     {
         // code ...
                    
          switch (choice)
          {
             case '0':
                  Environment.Exit(0);
                  break;
              case '1':
                  totalNumber = UserSelection("Total number of elements to be taken into account");
                  long permutation = IntMultiplication(1, totalNumber);
                  Console.WriteLine(totalNumber + "! = " + permutation);
                  break;
               case '2':
                  nbrSubAssy = UserSelection("Total number of elements in the subassy to be taken into account");
                  arrangement = IntMultiplication(totalNumber - nbrSubAssy + 1, totalNumber);
                  Console.WriteLine("A(" + totalNumber + "/" + nbrSubAssy + ") = " + arrangement);
                  break;
                case '3':
                   long combination = arrangement / IntMultiplication(1, nbrSubAssy);
                   Console.WriteLine("C(" + totalNumber + "/" + nbrSubAssy + ") = " + combination);
                    break;
                 default:
                     Console.WriteLine("Wrong input");
                      break;
            }
       }
    

    or in my opinion the better solution you could ask for the value in every case you need them