Home > Mobile >  Is there any way to run this code without getting the use of unassigned local variable error in the
Is there any way to run this code without getting the use of unassigned local variable error in the

Time:07-17

I tried to make a program in which the first option gets some input and the second one puts the output but I get an error which says that the 's' variable isn't assigned, is there anyway to do this without getting the error but getting the input every time i press 1?

  class Program
    {
        static void Main(string[] args)
        {
            string nume, prenume, facultate;
            int opt, varsta;
            double medieGenerala;
            do
            {
                Console.WriteLine("Introduceti optiunea dumneavoastra:");
                Console.WriteLine("0. Iesire");
                Console.WriteLine("1. Introduceti un nou student in catalog");
                Console.WriteLine("2. Afisati studentii din catalog");
                Console.WriteLine("3. Cautati un student dupa nume");
                opt = Convert.ToInt32(Console.ReadLine());
                switch(opt)
                {
                    case 0:
                        Environment.Exit(0);
                        break;
                    case 1:
{
                        Console.Write("Introduceti numele studentului:- ");
                        nume = Console.ReadLine();
                        Console.Write("Introduceti prenumele studentului:- ");
                        prenume = Console.ReadLine();
                        Console.Write("Introduceti varsta studentului:- ");
                        varsta = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Introduceti media generala a studentului:- ");
                        medieGenerala = Convert.ToDouble(Console.ReadLine());
                        Console.Write("Introduceti facultatea la care este studentul:- ");
                        facultate = Console.ReadLine();
                        Student[] s = new Student[]
                        {
                            new Student(nume,prenume,varsta,medieGenerala,facultate)
                        };
                        break;
}
                    case 2:
                        foreach(Student st in s)
                       {

                       }
                        break;
                }
            } while (true);
        }
    }
}

CodePudding user response:

Perhaps instead of declaring your Student array in the switch statement, you declare it outside of the statement. Then you can work with it in any case section.

Your code could be updated to something like:

            // create list prior to the switch statement
            List<Student> s = new List<Student>();

            do
            {
                Console.WriteLine("Introduceti optiunea dumneavoastra:");
                Console.WriteLine("0. Iesire");
                Console.WriteLine("1. Introduceti un nou student in catalog");
                Console.WriteLine("2. Afisati studentii din catalog");
                Console.WriteLine("3. Cautati un student dupa nume");
                opt = Convert.ToInt32(Console.ReadLine());

                switch(opt)
                {
                    case 0:
                        Environment.Exit(0);
                        break;
                    case 1:
                    {
                        Console.Write("Introduceti numele studentului:- ");
                        nume = Console.ReadLine();
                        Console.Write("Introduceti prenumele studentului:- ");
                        prenume = Console.ReadLine();
                        Console.Write("Introduceti varsta studentului:- ");
                        varsta = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Introduceti media generala a studentului:- ");
                        medieGenerala = Convert.ToDouble(Console.ReadLine());
                        Console.Write("Introduceti facultatea la care este studentul:- ");
                        facultate = Console.ReadLine();

                        // change array declaration to just add a new student to list here
                        s.Add(new Student(nume,prenume,varsta,medieGenerala,facultate));
                        break;
                    }
                    case 2:
                    {
                        foreach(Student st in s)
                       {
                          // now s is usable in here
                       }
                        break;
                    }
                }
            } while (true);

CodePudding user response:

Remember the default statement.

As it currently stands if neither of your case statements returns a true value the program will break. Every Switch statement needs a default value for when neither of the case statements returns a true value.

Also, it is best practice to always declare variables in the global scope so as to remove the chance of scope problems.

  •  Tags:  
  • c#
  • Related