-1
using System;
using System.Globalization;

namespace _13._Holidays_Between_Two_Dates
{
    class Program
    {
        static void Main(string[] args)
        {
            var startDate = DateTime.ParseExact(
              Console.ReadLine(), "d.m.yyyy", CultureInfo.InvariantCulture);

            var endDate = DateTime.ParseExact(
              Console.ReadLine(), "d.m.yyyy", CultureInfo.InvariantCulture);

            var holidaysCount = 0;

            // Problem: infinite loop
            for (var date = startDate; date <= endDate; date.AddDays(1))
            {
                if (date.DayOfWeek == DayOfWeek.Saturday || 
                    date.DayOfWeek == DayOfWeek.Sunday)
                {
                    holidaysCount++;
                }
            }

            Console.WriteLine(holidaysCount);
        }
    }
}

I am having troubles finding why iIcan't assign a value to the var date in the loop. Therefore the loop goes infinite. N00b

Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199

1 Answers1

3

Two issues:

  1. m stands for minutes, not months (M)
  2. date.AddDays(1) creates a new DateTime instance, you should assign it back to date: date = date.AddDays(1)

Should be

// m is minutes, M is months
var startDate = DateTime.ParseExact
  (Console.ReadLine(), "d.M.yyyy", CultureInfo.InvariantCulture);

// m is minutes, M is months
var endDate = DateTime.ParseExact(
  Console.ReadLine(), "d.M.yyyy", CultureInfo.InvariantCulture);

var holidaysCount = 0;

//DONE: Asssign date back: date = date.AddDays(1)
for (var date = startDate; date <= endDate; date = date.AddDays(1))
{
    if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
    {
        holidaysCount++;
    }
}
Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199