Programming Course in C# ¡Free!

Nested structs

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month and year. Finally, create an array of persons, ask the user for the dat of two persons and display them.

Output



Solution


using System;
class NestedStructs
{
struct person
{
public string Name;
public dateBirth Date;
}

struct dateBirth
{
public int Day;
public int Month;
public int Year;
}  

static void Main()
{   
person[] persons = new person[ 1 ];

for (int i = 0; i <= total; i++)
{
Console.Write("Enter name: ");
string name = Console.ReadLine();

persons[i].Name = name;

Console.Write("Enter day: ");
int d = Convert.ToInt32(Console.ReadLine());

persons[i].Date.Day = d;

Console.Write("Enter month: ");
int m = Convert.ToInt32(Console.ReadLine());

persons[i].Date.Month = m;

Console.Write("Enter year: ");
int y = Convert.ToInt32(Console.ReadLine());

persons[i].Date.Year = y;
}
}
}