Question
I keep getting the same errors and I'm not sure how to fix it. The errors: (Line 20) Field 'Date.dateString' must be fully assigned before
I keep getting the same errors and I'm not sure how to fix it.
The errors:
(Line 20) Field 'Date.dateString' must be fully assigned before control is returned to the caller.
(Line 20) Field 'Date.year' must be fully assigned before control is returned to the caller.
(Line 20) Field 'Date.month' must be fully assigned before control is returned to the caller.
(Line 20) Field 'Date.day' must be fully assigned before control is returned to the caller.
The question:
Write a program with a Date struct, that contains: Three fields: year, month, and day. All are int type. Two constructors: a. public Date(int year, int month, int day) - takes three parameters, and assigns corresponding values in the struct. b. public Date(string dateString). - takes a date string with format mm/dd/yyyy, such as 06/05/2017. You need to parse the string. Hint: Use dateString.Split('/') to split the string. See String.Split([] char) method doc. One method public void PrintInfomation() - It prints out the date information on the Console with the following format Today is Jun, 05, 2017. In the Main method, first read an instruction number from the console, with option of 1 and 2. 1) For option 1, the program creates a Date struct using constructor a. User will then type in date information separated by spaces ( ), as 2017 06 05. The date information is printed using PrintInfomation() method. 2) For option 2, the program creates a Date struct use constructor b. User will then type in date information in the format of 06/05/2017. The date information is printed using PrintInfomation() method. Example 1 (Blue lines are input; Black Bold is output): 1 2017 06 05 Today is Jun, 5, 2017 Example 2: 2 06/05/2017 Today is Jun, 5, 2017
My code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Date {
// Date struct struct Date { // defining parameters for constructors private int year; private int month; private int day; private string dateString;
// constructors public Date(int year, int month, int day) { // assign corresponding values in Date struct this.year = year; this.month = month; this.day = day; }
public Date(string dateString) { this.dateString = dateString;
// while dateString is not null while(dateString != "0") { // split dateString into date format 00/00/0000 string[] split = dateString.Split('/');
// date formate mm/dd/yyyy month = Convert.ToInt32(split[0]); day = Convert.ToInt32(split[1]); year = Convert.ToInt32(split[2]);
// break out of while statement break; } }
// print out date information public void PrintInformation() { System.Globalization.DateTimeFormatInfo mfi = new System.Globalization.DateTimeFormatInfo(); string moName = mfi.GetAbbreviatedMonthName(month).ToString(); Console.WriteLine("Today is " + moName + ", " + day + "," + year); } class Program { static void Main(string[] args) { // option chosen by user input int option;
// instruction number read from console Console.WriteLine(" Option 1: Date information seperated by spaces(' '), as 2017 06 05 Option 2: Date information seperated by slashes (/), as 06/05/2017"); Console.WriteLine(" Enter option 1 or 2: "); // read user input and convert to int option = Convert.ToInt32(Console.ReadLine());
string dateInfo;
// switch cases based on option number from user input switch(option) { case (1): Console.WriteLine("Enter year, month, and day separated by spaces (Example: 2017 06 05)"); dateInfo = Console.ReadLine(); while(dateInfo != "0") { // split date by space string[] split = dateInfo.Split(' '); int year = Convert.ToInt32(split[0]); int month = Convert.ToInt32(split[1]); int day = Convert.ToInt32(split[2]); // sent parameters to Date struct Date d1 = new Date(year, month, day); // print date information d1.PrintInformation(); break; } // end case 1 break;
case (2): Console.WriteLine("Enter month, day, and year separated by slashes (Example: 06/05/2017)"); dateInfo = Console.ReadLine(); Date d2 = new Date(dateInfo); d2.PrintInformation(); break;
default: Console.WriteLine("Invalid option. Thank you, bye! "); break; }
Console.ReadLine();
} } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started