2

i need guidance on how i can display for example all monday dates in a month, based on user input, can be any day and any month. my code so far is here but doesn't seem to display the dates. of the specified day i input, where has this gone wrong, any help is really appreciated.

using System;
using System.Globalization;

namespace Calendar
{
   class Program
   {
        static int promptDay = new int();
        static int year = new int();
        static int month = new int();
        static int[,] calendar = new int[6, 7];

        static void Main(string[] args)
        {
            Console.Write("Enter the year? ");
            year = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the month (January = 1, etc): ");
            month = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the day you want to see the dates for (Mon = 0, etc): ");
            promptDay = Convert.ToInt32(Console.ReadLine());
            DrawHeader();
            FillCalendar();
            DrawCalendar();
            Console.ReadLine();
        }

        static void DrawHeader()
        {
            Console.Write("\n\n");
            //gives you the month and year at the top of the calendar
            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month) + " " + year);
            Console.WriteLine("Mo Tu We Th Fr Sa Su");
        }

        static void FillCalendar()
        {
            int currentDay = 1;
            for (int i = 0; i < calendar.GetLength(0); i++)
            {
                for (int j = 0; j < calendar.GetLength(1) && currentDay <= totalDays; j++)
                {
                    if (i == 0 && day > j)
                    {
                        calendar[i, j] = 0;
                    }
                    else
                    {
                        if (j != promptDay)
                        {
                            calendar[i, j] = 0;
                        }
                        else
                        {
                            calendar[i, j] = currentDay;
                        }
                        currentDay++;
                    }
                }
            }
        }

        static void DrawCalendar()
        {
            for (int i = 0; i < calendar.GetLength(0); i++)
            {
                for (int j = 0; j < calendar.GetLength(1); j++)
                {
                    if (calendar[i, j] > 0)
                    {
                        if (calendar[i, j] < 10)
                        {
                            Console.Write(" " + calendar[i, j] + " ");
                        }
                        else
                        {
                            Console.Write(calendar[i, j] + " ");
                        }
                    }
                    else
                    {
                        Console.Write("   ");
                    }
                }
                Console.WriteLine("");
            }
        }
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
J.Doe
  • 55
  • 1
  • 1
  • 7
  • 1
    Possible duplicate of [How do i get all the weekdays and dates in a month](http://stackoverflow.com/questions/12903663/how-do-i-get-all-the-weekdays-and-dates-in-a-month) – Mong Zhu Dec 01 '16 at 15:22
  • Why are you declaring your `int`s like that? – ThePerplexedOne Dec 01 '16 at 15:23
  • yh i tried that but it doesnt ask for user input?? how can i get that? – J.Doe Dec 01 '16 at 15:23
  • i edited to new code i was trying earlier, – J.Doe Dec 01 '16 at 15:26
  • please check that and see where i have gone wrong – J.Doe Dec 01 '16 at 15:26
  • your for-loop never runs because you never assign `int days` any value. By default it is `0`. So no values are added into your list. Don't you want to ask the user once for the "weekday" ? and not on every iteration ? – Mong Zhu Dec 01 '16 at 15:30
  • the output should be similar to this --- – J.Doe Dec 01 '16 at 15:30
  • Which day do you want to look at (Mon = 0, etc)? 1 The Tuesdays of the month are: 4 11 18 25 – J.Doe Dec 01 '16 at 15:30
  • yes just once, and any day not just weekday – J.Doe Dec 01 '16 at 15:31
  • actually you could make your life very simple. Find the first day that is a Tuesday for example and then count up in steps of 7 until you overstep `totalDays` – Mong Zhu Dec 01 '16 at 15:32
  • i dont have to use tuesdays, i need the day and month based on user input and how could i implement what you have just suggested – J.Doe Dec 01 '16 at 15:34
  • i tried the duplicate you said at the start but it gives an error when i install console.write for user input to return weeday – J.Doe Dec 01 '16 at 15:37
  • So basically you want a method like `public static IEnumerable GetDays(int year, int month, DayOfWeek dayOfWeek) { DateTime value = new DateTime(year, month, 1); while (value.Month == month) { if (value.DayOfWeek == dayOfWeek) { yield return value.Day; } value = value.AddDays(1); } }, right?` – Corak Dec 01 '16 at 15:37
  • Or if you're uncomfortable with `IEnumerable` and `yield`, maybe: `public static List GetDays(int year, int month, DayOfWeek dayOfWeek) { List result = new List(); DateTime value = new DateTime(year, month, 1); while (value.Month == month) { if (value.DayOfWeek == dayOfWeek) { result.Add(value.Day); } value = value.AddDays(1); } return result; }` – Corak Dec 01 '16 at 15:40
  • i dont understand, can u just make some changes to the code i had given in the question – J.Doe Dec 01 '16 at 15:41
  • @J.Doe - I don't understand your code. But it seems like you *have* the `year` and the `month` and the `dayOfWeek` as user inputs and *want* to display the "day numbers" of each of the given `dayOfWeek`s in that month. – Corak Dec 01 '16 at 15:43
  • i changed the code based on people's suggestion, please look at the code now and my question – J.Doe Dec 01 '16 at 15:46
  • Well, your code doesn't compile. Please be sure to follow these suggestions: http://stackoverflow.com/help/mcve – Corak Dec 01 '16 at 15:50
  • i dont think you understood my question? is that correct – J.Doe Dec 01 '16 at 15:52
  • However, rather than displaying a calendar, you should ask for a different day and output which dates in the month fall on that day. An example of the output from this program could be: Enter the day the first falls on (Mon = 0, etc): 5 How many days in the month? 31 Which day do you want to look at (Mon = 0, etc)? 1 The Tuesdays of the month are: 4 11 18 25 – J.Doe Dec 01 '16 at 15:53
  • ^^^ i need to do this – J.Doe Dec 01 '16 at 15:53
  • @J.Doe - Well, if it's not "you *have* the `year` and the `month` and the `dayOfWeek` as user inputs and *want* to display the 'day numbers' of each of the given `dayOfWeek`s in that month.", then no, I don't understand your question. – Corak Dec 01 '16 at 15:54
  • ok i'll explain again, so basically i need user input on days in a month and month name, and day (mon,tues,wed,etc..) and then the ouput should display the dates for the day specified by the user, for example if the user entered monday and november the output should display the dates for the monday in november – J.Doe Dec 01 '16 at 15:57
  • @J.Doe - ah, you see? "Enter the day the first falls on (Mon = 0, etc)" is quite different from what you have. – Corak Dec 01 '16 at 15:57
  • yh i am sorry if it is a mess of a code, i am just a beginner – J.Doe Dec 01 '16 at 15:58

1 Answers1

2

The following code prints out all the days for a given DayOfWeek, yes it can be improved but it's just a starting point

OutPut

What is the year? : 2016
What is the name of the month? : dicembre
Monday
5 12 19 26
Thursday
1 8 15 22 29
Wednesday
7 14 21 28
Tuesday
6 13 20 27
Friday
2 9 16 23 30
Saturday
3 10 17 24 31
Sunday
4 11 18 25

Note that dicembre is december in Italian.

Dictionary<DayOfWeek, List<int>> daysCount = new Dictionary<DayOfWeek, List<int>>()
{
    { DayOfWeek.Monday, new List<int>() },
    { DayOfWeek.Thursday, new List<int>() },
    { DayOfWeek.Wednesday, new List<int>() },
    { DayOfWeek.Tuesday, new List<int>() },
    { DayOfWeek.Friday, new List<int>() },
    { DayOfWeek.Saturday, new List<int>() },
    { DayOfWeek.Sunday, new List<int>() },
};

Console.Write("What is the year? : ");
var year = Convert.ToInt32(Console.ReadLine());
Console.Write("What is the name of the month? : ");
var monthName = Console.ReadLine();

var monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month;
var dayOfMonth = DateTime.DaysInMonth(year, monthNumber);

var date = new DateTime(year, monthNumber, 1);


for(int i = 0; i < dayOfMonth; i++, date = date.AddDays(1))
{

    if (daysCount[date.DayOfWeek] == null)
    {
        daysCount[date.DayOfWeek] = new List<int>();
    }

    daysCount[date.DayOfWeek].Add(date.Day);
}

foreach (var day in daysCount)
{
    Console.WriteLine(day.Key.ToString());
    foreach (var dayNumber in day.Value)
    {
        Console.Write(string.Format("{0} ", dayNumber));
    }
    Console.WriteLine();

}

Console.ReadLine();

Variant to show only user specified day

Notice that Sunday = 0

var days = new List<int>();
Console.Write("What is the year? : ");
var year = Convert.ToInt32(Console.ReadLine());
Console.Write("What is the name of the month? : ");
var monthName = Console.ReadLine();
Console.Write("Enter the day you want to see the dates for (Mon = 0, etc): ");
var promptDay = Convert.ToInt32(Console.ReadLine());

var monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month;
var dayOfMonth = DateTime.DaysInMonth(year, monthNumber);

var date = new DateTime(year, monthNumber, 1);


for(int i = 0; i < dayOfMonth; i++, date = date.AddDays(1))
{

    if ((int)date.DayOfWeek == promptDay)
        days.Add(date.Day);
}

foreach (var day in days)
{
    Console.Write(string.Format("{0} ", day));

}

Console.ReadLine();
Sid
  • 14,176
  • 7
  • 40
  • 48