0

My array of objects when printed only returns the value of the last group of objects entered. so if i enter a set of values in at the end it will only print out the last set of data.

namespace classschedule { class Program {

    static void Main(string[] args)
    {

        Course[] newarray = new Course[25];

        Course[ , ] mycoursearray = new Course[6,5];
        Course info = new Course();
        int sections, classregistration, periods;
        string names, classday;

        for (int x1 = 0; x1 < 6; x1++)
        {
            for (int y1 = 0; y1 < 5; y1++)
            {

                mycoursearray[x1, y1] = new Course();
            }
        }


        for (int x1 = 0; x1<5;x1++)
        {
            newarray[x1] = new Course();
        }

        int x = 0;
        int y = 0;
        for (int g = 0; g <= 4; g++)
        {
            for (int h = 0; h <= 5; h++)
            {
                info.busy = 0;
                info.section = 0;
                info.classize = 0;
                info.classname = "";
                mycoursearray[h, g] = info;
            }

        }




        for (int j = 0; j < 24; j++)
        {



            Console.WriteLine("please enter class name: ");
            names = Console.ReadLine();


            char choice = 'y';
            Console.WriteLine("please enter the section of your class: ");
            sections = Convert.ToInt32(Console.ReadLine());
           while (choice == 'y' || choice == 'Y')
            { if (sections <= 90)
                {

                    choice = 'n';
                }
            else
                {
                    Console.WriteLine("Section is to high must be below 90");

                    choice = 'n';



                }                                             
            }

            Console.WriteLine("Please enter the amount of students in the class: ");
            classregistration = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("Please enter the first letter of the day of the class **for thursday please type X");
            classday = Console.ReadLine();


            Console.WriteLine("Please enter your class period");
            periods = Convert.ToInt32(Console.ReadLine());

            x = periods - 1 ;
            if (classday == "M")
                y = 0;
            else if (classday == "T")
                y = 1;
            else if (classday == "W")
                y = 2;
            else if (classday == "X")
                y = 3;
            else if (classday == "F")
                y = 4;

                info.classname = names;
                info.section = sections;
                info.classize = classregistration;
                info.busy = 1;
                mycoursearray[x, y] = info;
                newarray[y+1] = info;






        }

    {
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 5 ; j++)
            {
                Console.Write(mycoursearray[i, j].classname +"  " + mycoursearray[i, j].section+ "   " + mycoursearray[i, j].classize);
                //Console.Write(mycoursearray[i, j].section);
                //Console.Write(mycoursearray[i, j].classize);
            }
                Console.WriteLine();
        }
    }                      






        Console.ReadLine();






    }
    class Course
    {

        public int section;
        public string classname;
        public int classize;
        public int busy;


    }
}

}

  • consider using s debugger. – Ousmane D. Apr 03 '17 at 01:48
  • Your code correctly initializes both `mycoursearray` and `newarray` by instantiating new `Course` objects for each element. Then the next thing you do is discard all that work by copying the `info` reference into each array element. Later, when you take use input, you do the same thing. So all of the array elements are pointing to the same object. See marked duplicates for additional information. – Peter Duniho Apr 03 '17 at 01:58

0 Answers0