PreslavMihaylov
PreslavMihaylov

Reputation: 55

relevant loop and leap year c#

I am going to say in advance, that I am a beginner in programming and this question might seem quite irrelevant. However, I truly wonder how to proceed in this situation.

This is my code:

string startdate;

Console.WriteLine("Please, type in your birthdate (dd-mm-yyyy)");
startdate = Console.ReadLine();
DateTime bday = DateTime.Parse(startdate);
        // prob 1.
DateTime now = DateTime.Now;
TimeSpan ts1 = now.Subtract(bday);
DateTime dt = new DateTime(0001, 01, 01);
TimeSpan ts2 = new TimeSpan(365, 0, 0, 0);
        //prob2.
TimeSpan ts3 = new TimeSpan(3650, 0, 0, 0);
dt = dt + ts1 - ts2;
Console.WriteLine("Your current age is:{0}", dt.ToString("yy"));
dt = dt + ts3;
Console.WriteLine("Your Age after 10 years will be:{0}", dt.ToString("yy"));

Problem 1: I would like to create a loop where if the info that is given in the console is different from dd-mm-yyyy, to repeat the whole process again.

Problem 2: I would like to see whether the next year(from the current one) is a leap year, and thus know whether ts2 should be 365 days or 366.

Thank you in advance.

Upvotes: 1

Views: 764

Answers (3)

Antoon Meijer
Antoon Meijer

Reputation: 89

Problem 1:

That can be solved using a while loop.

while(!DateTime.Parse(startdate))// The "!" is for NOT
{
    Console.WriteLine("Incorrect format please type your birthday again(dd-mm-yyyy)");
    startdate = Console.ReadLine();
}

However this brings up another problem DateTime.Parse will throw an error when the string is incorrect.(http://msdn.microsoft.com/en-us/library/1k1skd40%28v=vs.110%29.aspx)

In order to solve this you need to do use a try catch clause in order to "catch" the error.

See more details here(http://msdn.microsoft.com/en-us/library/0yd65esw.aspx) Therefore the code will look like this:

bool isCorrectTime = false;
while(!isCorrectTime) // The "!" is for NOT
{
    try
    {
        Console.WriteLine("Incorrect format please type your birthday again(dd-mm-yyyy)");
        startdate = Console.ReadLine();
        isCorrectTime = true; //If we are here that means that parsing the DateTime
        // did not throw errors and therefore your time is correct!
    }
    catch
    {
        //We leave the catch clause empty as it is not needed in this scenario
    }

}

For problem 2 see Steve's answer.

Upvotes: 0

Steve
Steve

Reputation: 216313

The Leap year problem is not really a problem thanks to the Framewrok

int daysToAdd = 0;
for(int i = 1; i <= 10; i++)
   daysToAdd += (DateTime.IsLeapYear(DateTime.Today.Year + i) ? 366 : 365);

The first problem could be solved with

DateTime inputDate;
while(true)
{
    Console.WriteLine("Please, type in your birthdate (dd-mm-yyyy)");
    string startdate = Console.ReadLine();
    if(DateTime.TryParseExact(startdate, "dd-MM-yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out inputDate))
        break;
}

Upvotes: 0

Richard
Richard

Reputation: 109100

Re. Problem 1:

Take a look at DateTime.TryParseExact: this allows you to specify a format, and rather than throwing an exception returns false on the input format not matching. Thus

DateTime res;
String inp;
do {
  inp = Console.ReadLine("Date of birth: ");
} while (!DateTime.TryParseExact(inp, "yyyy-MM-dd", CultureInfo.CurrentCulture, DateTimeStyles.None, out res));

Re, Problem 2: See DateTime.AddYears as noted in the comments on the Q.

Upvotes: 1

Related Questions