• C# program using an Array of Structs

    Miranda65M Member

    Write a C# program that receives the following information from a set of students:

    Student Id:
    Student Name:
    Course Name:
    Date of Birth:

    The application should also display the information of all the students once the data is entered.

    Implement this using an Array of Structs.

  • Abhey Member

    i did the code with a friends assistance….but i haven’t completely understood it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace onef
    {
    public struct onef
    {
    public int studentid;
    public string studentname;
    public string coursename;
    public int dob;
    
    
    public void get()
    {
    Console.WriteLine("Enter Student ID:");
    studentid = int.Parse(Console.ReadLine());
    
    Console.WriteLine("Enter Student Name:");
    studentname = Console.ReadLine();
    
    Console.WriteLine("Enter Course Name:");
    coursename = Console.ReadLine();
    
    Console.WriteLine("Enter Date of Birth:");
    dob = int.Parse(Console.ReadLine());
    }
    
    public void display()
    {
    Console.WriteLine("ID >> " + studentid);
    Console.WriteLine("Name >> " + studentname);
    Console.WriteLine("Course >> " + coursename);
    Console.WriteLine("DOB >> " + dob);
    }
    };
    
    public class program
    {
    public static void Main(string[] args)
    {
    onef[] obj = new onef[10];
    int i;
    
    for (i = 0; i < 10; i++)
    {
    obj[i].get();
    obj[i].display();
    }
    }
    }
    }
    
  • ShikhaTan Member

    I’ve altered your program slightly, but I didn’t run it, so it isn’t tested. But it should work.

    In my opinion, it’s better (and easier) to use lists instead of arrays, so I build my example on lists, which will be converted to an array later on. This isn’t my best code either. I used most of your code. (And it’s been a while since I’ve coded C# Razz)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace onef
    {
        public struct onef
        {
            public int studentid;
            public string studentname;
            public string coursename;
            public int dob;
    
            public void display()
            {
                Console.WriteLine("ID >> " + studentid);
                Console.WriteLine("Name >> " + studentname);
                Console.WriteLine("Course >> " + coursename);
                Console.WriteLine("DOB >> " + dob);
            }
        };
    
        public class program
        {
            private List list = new List();
            public static void Main(string[] args)
            {
                //Get the information from the user
                for(int i = 0; i < 10; i++)
                {
                    get();
                }
               
                onef[] students = list.toArray();
               
                for(int i = 0; i < students.Length; i++)
                {
                    students[i].display();
                }
            }
    
            public void get()
            {
                //Save the values in temporary variables.
                int studentid;
                string studentname;
                string coursename;
                int dob;
    
                //Get the values from the user
                Console.WriteLine("Enter Student ID:");
                studentid = int.Parse(Console.ReadLine());
    
                Console.WriteLine("Enter Student Name:");
                studentname = Console.ReadLine();
    
                Console.WriteLine("Enter Course Name:");
                coursename = Console.ReadLine();
    
                Console.WriteLine("Enter Date of Birth:");
                dob = int.Parse(Console.ReadLine());
    
                //Add the values to the list.
                addToList(studentid, studentname, coursename, dob);
            }
           
            private void addToList(int studentid, string studentname, string coursename, int dob)
            {
                //Could be done better, but I just used most of your code
                onef newStudent = new onef();
                onef.studentid = studentid;
                onef.studentname = studentname;
                onef.coursename = coursename;
                onef.dob = dob;
    
                //Add it to the list
                list.add(onef);
            }
        }
    }
    
  • Amit Member

    The code itself is very messy and not really easy to understand. I think you should go back and rethink your design and requirements. Write code, iterate and experiment. Breaking down problems is the most important skill you can develop as a programmer. Simple code is maintainable, fast and far more likely to be correct than complex structures.

    Let’s see what we want here;
    Enter student data
    Print registered students (which means we need to save it somehow)

    Start from the simplest program;
    First iteration
    User can enter student information.
    Application quits.

    Second iteration
    User can select to enter student information or quit application
    While user has not selected quit, let user enter student information

    Third iteration
    User can select to enter student information or quit application (Display how many students are registered)
    While user has not selected quit, let user enter student information and save to array

    Note about this iteration, I’ve not made it to print the student array because it’s too big of a requirement for a single iteration. Instead I’ve made a simpler request (to display the total of registered students) which will force us to somehow save the students into an array.

    Fourth iteration
    User can select to enter student information, print registered students’ information or quit application (Display how many students are registered)
    While user has not selected quit
    if user selected to enter information let user enter student information and save to array
    if user selected to print student information, for each student entry in the students array print information

    And well, that’s it. Each iteration builds on the previous one (you’ll need to refactor and refine code as you go though). For example, the first iteration will not need any methods other than the main one but for the second iteration we see that we want to perform the same thing over and over again so it’s better to refactor it into another method. For the third iteration we see that we need to save our data but then how do we return all that information from our method? make a container structure (struct) for the information so we can pass it around as a single logical unit…

    And one last thing, one of the most important things in your code are the names you give to everything that is used, from classes, structs and variables to methods and namespaces. Learn about camel and pascal cases or separate using underscores but make sure that everything is readable and understandable. For example, what the heck is “onef” namespace??

  • SapnaVishwas Member
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AOS
    {
        class Program
        {
            public struct Student
            {
                public int StudentID;
                public String StudentName;
                public String CourseName;
                public DateTime DOB;
            }
            static void Main(string[] args)
            {
                Student[] StudArr = new Student[0];
                Console.WriteLine("How many students are there?");
                Array.Resize(ref StudArr, int.Parse(Console.ReadLine()));
                for (int i = 0; i < StudArr.Length; i++)
                {
                    StudArr[i] = Get();
                }
                for (int i = 0; i < StudArr.Length; i++)
                {
                    Display(StudArr[i]);
                }
                Console.WriteLine("We're done!");
                Console.ReadLine();
    
            }
            static public Student Get()
            {
                Console.WriteLine("New student entry");
                Student std = new Student();
                Console.WriteLine("Please enter the student's ID");
                std.StudentID = int.Parse(Console.ReadLine());
                Console.WriteLine("Please enter the student's name");
                std.StudentName = Console.ReadLine();
                Console.WriteLine("Please enter the course's name");
                std.CourseName = Console.ReadLine();
                Console.WriteLine("Please enter the student's DOB");
                std.DOB = DateTime.Parse(Console.ReadLine());
                Console.WriteLine("Thanks!");
                Console.WriteLine();
                return std;
    
            }
            static public void Display(Student std)
            {
                Console.WriteLine("ID: " + std.StudentID);
                Console.WriteLine("Name: " + std.StudentName);
                Console.WriteLine("Course name: " + std.CourseName);
                Console.WriteLine("DOB: " + std.DOB.ToString());
            }
    
        }
    }
    
Viewing 4 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish