Home > Enterprise >  How to save user input into an array
How to save user input into an array

Time:12-07

How to save user input into an array

I have two modules

MODULE director: Allows you to add and remove teachers. MODULE Informer: Gives information about the teachers available in the school. All this in an endless loop.

To the code: I have an initial array of teachersForDirector, consisting of 10 teachers. If the user wants to add or remove a teacher, I use the DeletingTeachers, AddingTeachers methods. The problem is that when I select the "informer" module, the values of the teachersForDirector array are reset to the original.

Code:

public static List<string> DeletingTeachers(List<string> arrayTeachers)
        {
            int counter = 0;

            Console.WriteLine("which teacher to remove? [index]");

            for (int i = 0; i < arrayTeachers.Count; i  )
            {
                Console.WriteLine($"teacher with number - {counter} {arrayTeachers[i]}");
                counter  ;
            }
            int index = int.Parse(Console.ReadLine());
            arrayTeachers.RemoveAt(index);
            Console.Clear();
            for (int i = 0; i < arrayTeachers.Count; i  )
            {
                Console.WriteLine($"new teachers - {arrayTeachers[i]}");
            }
            return arrayTeachers;
        }
        public static List<string> AddingTeachers(List<string> arrayTeachers)
        {

            Console.WriteLine("enter the name of the new teacher");
            arrayTeachers.Add(Console.ReadLine());
            
            for (int i = 0; i < arrayTeachers.Count; i  )
            {
                Console.WriteLine($"{arrayTeachers[i]}");
            }
            Console.Clear();
            Console.WriteLine("teachers information:"); 
            for (int i = 0; i < arrayTeachers.Count; i  )
            {
                Console.WriteLine($"{arrayTeachers[i]}");
            }
            return arrayTeachers;
        }
   
      static void Main(string[] args)
        {
            while (true)
            {
                List<string> teachersForDirector = new List<string> { "Матвеева Н.В", "Ивашина А.С", "Изюмов Р.Н.", "Жиделев А.С.", "Карпов М.Д", "Петрова О.А", "Таран Г.Г.", "Овчарова Д.Е.", "Андреев Д.Е.", "Долгих Н.О." };

                Console.WriteLine("Choose:\ndirector - 0 \ninformer - 1"); // MAIN MENU                
                int DirectorZeroInformerOne = int.Parse(Console.ReadLine());
                Console.Clear();
                if (DirectorZeroInformerOne == 0) // changing teachers
                {
                    {

                        Console.WriteLine("вы хотите удалить учителя[1] или добавить нового[2]?");
                        int chooseDeleteOrNew = int.Parse(Console.ReadLine());
                        Console.Clear();
                        if (chooseDeleteOrNew == 1) // removing a teacher
                        {
                            DeletingTeachers(teachersForDirector);
                        }
                        if (chooseDeleteOrNew == 2) // adding a teacher
                        {
                            AddingTeachers(teachersForDirector);
                        }
                    }
                }
                if (DirectorZeroInformerOne == 1)
                {
                    Console.WriteLine("information about teachers");
                    for (int i = 0; i < teachersForDirector.Count; i  )
                    {
                        Console.WriteLine(teachersForDirector[i]);
                    }
                }
            }
        }

How do I make sure that the changed data is saved and the informer module outputs the correct information?

CodePudding user response:

I put your question into "ChatGPT" chat bot. This is what it replied:

To save the changes made to the teachersForDirector list, you need to make sure that you are modifying the same list object in all methods. In your current implementation, you are creating a new instance of the teachersForDirector list every time the Main method is called.

To fix this, you can move the declaration and initialization of the teachersForDirector list outside of the Main method, so that it is only created once. Then, you can pass this list as an argument to the DeletingTeachers and AddingTeachers methods, which will modify the same list object. Here is an example of how you could modify your code to achieve this:

`public static List DeletingTeachers(List arrayTeachers) { int counter = 0;

    Console.WriteLine("which teacher to remove? [index]");

    for (int i = 0; i < arrayTeachers.Count; i  )
    {
        Console.WriteLine($"teacher with number - {counter} {arrayTeachers[i]}");
        counter  ;
    }
    int index = int.Parse(Console.ReadLine());
    arrayTeachers.RemoveAt(index);
    Console.Clear();
    for (int i = 0; i < arrayTeachers.Count; i  )
    {
        Console.WriteLine($"new teachers - {arrayTeachers[i]}");
    }
    return arrayTeachers;
}

public static List<string> AddingTeachers(List<string> arrayTeachers)
{
    Console.WriteLine("enter the name of the new teacher");
    arrayTeachers.Add(Console.ReadLine());

    for (int i = 0; i < arrayTeachers.Count; i  )
    {
        Console.WriteLine($"{arrayTeachers[i]}");
    }
    Console.Clear();
    Console.WriteLine("teachers information:"); 
    for (int i = 0; i < arrayTeachers.Count; i  )
    {
        Console.WriteLine($"{arrayTeachers[i]}");
    }
    return arrayTeachers;
}

static void Main(string[] args)
{
    // Declare and initialize the teachersForDirector list here
    List<string> teachersForDirector = new List<string> { "Матвеева Н.В", "Ивашина А.С", "Изюмов Р.Н.", "Жиделев А.С.", "Карпов М.Д", "Петрова О.А", "Таран Г.Г.", "Овчарова Д.Е.", "Андреев Д.Е.", "Долгих Н.О." };

    while (true)
    {
        Console.WriteLine("Choose:\ndirector - 0 \ninformer - 1"); // MAIN MENU                
        int DirectorZeroInformerOne = int.Parse(Console.ReadLine());
        Console.Clear();
        if (DirectorZeroInformerOne == 0)

`

Cool!

  •  Tags:  
  • c#
  • Related