An example of an array of structures

I'm a little new to structures in C # ..

My question says:

Write a console application that receives the following information for enrolling students: student, student, course, date of birth. The application should also display the entered information. Implement this with structures.

I went up to this β†’

struct student { public int s_id; public String s_name, c_name, dob; } class Program { static void Main(string[] args) { Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); s_id = Console.ReadLine(); s_name = Console.ReadLine(); c_name = Console.ReadLine(); s_dob = Console.ReadLine(); student[] arr = new student[4]; } } 

Please help me after this.

+4
source share
2 answers

You started correctly - now you just need to populate each student structure in the array:

 struct student { public int s_id; public String s_name, c_name, dob; } class Program { static void Main(string[] args) { student[] arr = new student[4]; for(int i = 0; i < 4; i++) { Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); arr[i].s_id = Int32.Parse(Console.ReadLine()); arr[i].s_name = Console.ReadLine(); arr[i].c_name = Console.ReadLine(); arr[i].s_dob = Console.ReadLine(); } } } 

Now just try again and write this information to the console. I will let you do this, and I will let you try to make a program to accept any number of students, not just 4.

+11
source

For an instance of the structure, you set the values.

  student thisStudent; Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); thisStudent.s_id = int.Parse(Console.ReadLine()); thisStudent.s_name = Console.ReadLine(); thisStudent.c_name = Console.ReadLine(); thisStudent.s_dob = Console.ReadLine(); 

Please note that this code is incredibly fragile as we don’t check user input at all. And you did not understand the user that you expect each data point to be entered on a separate line.

0
source

Source: https://habr.com/ru/post/1501930/


All Articles