user8706025
user8706025

Reputation:

Simple Calendar with option for different months

Here's the prompt: Write a program that prints a calendar for one month. Input consists of an integer specifying the first day of the month (1 = Sunday) and an integer specifying how many days are in a month.

Here is a sample output of what it should look like:

    First day of the month 3 
    Number of days in the month 31

    Sunday Monday Tuesday Wednesday Thursday Friday Saturday
                     1        2        3       4       5 
      6      7       8        9        10      11      12
      13     14      15       16       17      18      19
      20     21      22       23       24      25      26
      27     28      29       30       31

I've been working on it and here is what I have so far.

namespace Program_202t
{
   class Program
   {

     static void Main(string[] args)
     {
        Console.Title = "Program 202t";

        Console.Write("Enter the first day of the month: ");
        int year = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of days in a month: ");
        int month = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("\nSunday  Monday   Tuesday  Wedsnesday  Thursday  Friday  Saturday");
        if (year == 1)
        {
            Console.SetCursorPosition(0, 4);
            for (int i = 1; i <= 10; i++)
            {
                Console.Write(i + "         ");


                if (((i) % 7) > 0)
                {
                }

                else
                {
                    Console.Write("\n");
                }
            }
            Console.Write("\b");
            for (int b = 11; b <= month; b++)
            {
                if (((b) % 7) > 0)
                {
                    Console.Write(b + "        ");
                }
                else
                {
                    Console.Write(b + "\n");
                }

            }

        }
        if (year == 2)
        {
            //Console.SetCursorPosition(10, 4);
            for (int i = 1; i <= month; i++)
            {
                if (i == 1)
                {
                    Console.Write("         " + i + "         ");
                }
                else if (i > 1 && i < 11 && i != 6)
                {
                    Console.Write(i + "         ");
                }
                Console.Write("\b");
                if (i == 6)
                {
                    Console.Write(i + "\n");
                }
                if (i > 11 && i != 14)
                {
                    Console.Write(i + "       ");
                }
                if (i == 14)
                {
                    Console.Write(i + "\n");
                }


                /*if (((i) % 6) > 0)
                {
                }

                else
                {
                    Console.Write("\n");
                }
            }
            Console.Write("\b");
            //FIX!!!
            for (int b = 11; b <= month; b++)
            {
                if (((b) % 13) > 0 && b >13)
                {
                    Console.Write(b + "        ");

                }
                if (((b) % 20) > 0 && b <= 13)
                {
                    Console.Write(b + "        ");
                }
                else
                {
                    Console.Write("\n");
                }

            }*/

            }
            if (year == 3)
            {
                Console.SetCursorPosition(20, 4);
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 5) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write("\n");
                    }

                }

            }
            if (year == 4)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            if (year == 5)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            if (year == 6)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            if (year == 7)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            Console.ReadLine();
        }


    }

}

Do any of you know what I am doing wrong for the years that aren't 1? One works fine, but the rest have various problems.

Thanks for the help!

Upvotes: 0

Views: 745

Answers (2)

Artem Shkumat
Artem Shkumat

Reputation: 41

Will this do?

 class Program
{
    static void Main(string[] args)
    {
        Console.Title = "Program 202t";

        Console.Write("Enter the first day of the month: ");
        int startingDay = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of days in a month: ");
        int daysInMonth = Convert.ToInt32(Console.ReadLine());

        List<string> daysOfTheWeek = new List<string>() {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        foreach (string day in daysOfTheWeek)
        {
            Console.Write($"{day,10}");
        }
        List<string> days = new List<string>();
        for (int i = 0; i < startingDay; i++)
        {
            days.Add($"{"",10}");
        }
        for (int i = 1; i < daysInMonth+1; i++)
        {
            days.Add($"{i,10}");
        }
        for (int i = 0; i < days.Count; i++)
        {
            if (i%7!=0) {Console.Write(days[i]);}
            else {Console.WriteLine(days[i]);}                
        }

    }

Upvotes: 1

Eissa
Eissa

Reputation: 700

Alrighty, i did a quick run through of the code and I see where you're issues are so I'll give you some tips on where to look.

For one, your code has logical issues revolving around the required formatting of the output. There are simpler ways to output the correct formatting. If you look at the .NET String Reference then look at formatting, you may find an easier way to do it.

Next, you have an issue with misaligned block endings so some of your code is never getting executed. I recommend you breakpoint your code at the first if statement and step through the code to understand the execution in runtime.

Lastly, I recommend clarifying your variable names a little bit. If the first input represent that day of the week, then perhaps use a variable name that represents it (for example dayOfWeek, but month doesn't do it for me). Also the number of days in the month could use a better variable name.

Please do ask questions if you need some more guidance. I'd be happy to help.

Upvotes: 0

Related Questions