1
Answer

Having trouble using the "DateTime" line in VS 2015

Ask a question
Kevin Ware

Kevin Ware

8y
598
1

So I've been assigned a little project in which I need to take information from students, enter it into a simple console application, and when asked to continue, hit no, and then boom, all of the information magically appears in the window. I'm asking for simple information:

 First Name, Last Name, DOB, Student ID, Number of Classes, Graduation Year.

 I have everything down except for the DOB. I can't get it to display without the application crashing. The names are <strings> and the numbers are <int> but for the DOB I need to use "DateTime".
 
Here's what I have so far, I don't have the DOB stuff entered in anymore because it was causing a crash.
 
 
 
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace MidtermWeek5

{

   class Program

   {

       static void Main(string[] args)

       {

            //VARIABLE AREA

            bool isContinued = true;

            int listCount = 0;

            List<string> firstName = new List<string>();

            List<string> lastName = new List<string>();

            List<int> studentID = new List<int>();

            List<int> classCount = new List<int>();

            List<int> graduateYear = new List<int>();

            while (isContinued)

            {

                 //COLLECT DATA AREA

                 Console.WriteLine("Please enter your first name:");

                 firstName.Add(Console.ReadLine());

                 Console.WriteLine("Please enter your last name:");

                 lastName.Add(Console.ReadLine());

                 Console.WriteLine("Please enter your student ID (XXXX):");

                 studentID.Add(Convert.ToInt32(Console.ReadLine()));

                 Console.WriteLine("Please enter your number of classes:");

                 classCount.Add(Convert.ToInt32(Console.ReadLine()));

                 Console.WriteLine("Please enter your graduation year:");

                 graduateYear.Add(Convert.ToInt32(Console.ReadLine()));

                 //CHECK USER TO CONTINUE

                 Console.WriteLine("Do you want to continue (Y/N)?");

                 if (Console.ReadLine().ToUpper() == "Y")

                 {

                      isContinued = true;

                      Console.Clear(); //clear screen

                 }

                 else

                 {

                      isContinued = false;

                 }

             }

             //DISPLAY RESULTS AREA

             Console.WriteLine("{0, -9} {1, -8} {2, -10} {3, -8} {4, -15}", "First", "Last", "Student ID", "Classes", "Graduate Year");

             Console.WriteLine("-------------------------------------------------------------------------------");

             listCount = firstName.Count;

             while (listCount > 0)

             {

                  Console.WriteLine("{0, -9} {1, -8} {2, -10} {3, -8} {4, -15}", firstName[listCount - 1], lastName[listCount - 1], studentID[listCount - 1], classCount[listCount - 1], graduateYear[listCount - 1]);

listCount--;

             }

             //KEEP THE SCREEN ON

             Console.ReadLine();

          }

      }

}

 
 
 
 
 
 
 
 
I was trying:
 
DateTime birthday;
and 
List<DateTime> birthday = new List<DateTime>();
 
and for collecting the information:
 
Console.WriteLine("Please enter your DOB:");
birthday.Add(Convert.ToDateTime(Console.ReadLine()));
 
and for displaying the information:
 

Console.WriteLine("{0, -9} {1, -8}  {2, -12}  {3, -10} {4, -8} {5, -15}", "First", "Last", "DOB", "Student ID", "Classes", "Graduate Year");

Console.WriteLine("-------------------------------------------------------------------------------");

listCount = firstName.Count;

while (listCount > 0)

{

Console.WriteLine("{0, -9} {1, -8}  {2, -12}  {3, -10} {4, -8} {5, -15}", firstName[listCount - 1], lastName[listCount - 1], birthday[listCount - 1], studentID[listCount - 1], classCount[listCount - 1], graduateYear[listCount - 1]);

listCount--;

}

 
 
Please help!
 

Answers (1)