-6

Im trying to write a C# console program to read in the number of a month and output the name of the month, and then ask the user if they want to know the number of days in that month and if so output the number of days. Assuming that there are no leap years and February ALWAYS has 28 days only.

Thanks in advance if anyone can help!!

EDIT:

This is what I have so far, I'm having trouble with the second half of the problem, I'm not sure how to ask the user if they want to know the days of the month and how to use a switch to output the number of days...

class MainClass
{
  public static void Main (string[] args)
  {
    {
      Console.WriteLine("Give me an integer between 1 and 12, and I will give you the month");

      int monthInteger = int.Parse(Console.ReadLine());

      DateTime newDate = new DateTime(DateTime.Now.Year, monthInteger, 1);

      Console.WriteLine("The month is: " + newDate.ToString("MMMM"));
      Console.WriteLine();
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
stringname
  • 7
  • 1
  • 4
  • 4
    Have you done ANYTHING yourself yet? do you have any code you already have? write some code and come back when you have a specific problem. – Nzall Jul 28 '14 at 10:22
  • 1
    This is a duplicate of this: http://stackoverflow.com/questions/975531/how-to-get-the-monthname-in-c – Lasse V. Karlsen Jul 28 '14 at 11:25
  • Welcome to SO! Thank you for following up by posting the code that you tried. Please keep in mind that the folks who try to help you here want to help, but they also want to promote knowledge and best practices. Showing your code helps us do this. Note that in this example there are better built-in methods for getting info about dates, but since this is clearly a canned academic exercise to teach you about `switch` statements I have provided an example solution below. Please take the time to understand and play with the code before turning it in. – nothingisnecessary Jul 28 '14 at 17:03

4 Answers4

3

A simple switch case will do?

string input = Console.In.ReadLine();
int number = -1;
int.TryParse(input, out number);
switch (number)
{
    case 1:
        Console.Out.WriteLine("January");
        break;
    case 2:
        Console.Out.WriteLine("February");
        break;
    case -1:
        Console.Out.WriteLine("Please input a valid number");
        break;
    default:
        Console.Out.WriteLine("There are only 12 months in a year");
        break;
}

i take it this is enough to finish the rest of your code.

next time, please provide some code for what you have tried already, just asking for simple code usually gets you nowhere.

Vincent
  • 1,497
  • 1
  • 21
  • 44
  • Thank you, I've had a go at the first half of the problem but I'm not sure how to do the second half using a switch..I've put my code up the top – stringname Jul 28 '14 at 12:35
1
public static string getName(int i)
{
    string[] names = { "jan", "feb", ... } // fill in the names
    return names[i-1];
}
Exceptyon
  • 1,584
  • 16
  • 23
  • Is that using a switch? I have to use a switch in my program – stringname Jul 28 '14 at 13:17
  • no if you want to use a switch statement use the answer by @Vincent Advocaat, he is correct. This is "the usual way to translate from one constant to another" (int number of month -> string name). Another possibile way is with a Dictionary. Go with that answer =) – Exceptyon Jul 28 '14 at 13:25
1
public static string getMonthName(int mounth)
{
    DateTime dt = new DateTime(2000, mounth, 1);
    return dt.ToString("M").Substring(0, dt.ToString("M").IndexOf(' '));
}
AsfK
  • 3,328
  • 4
  • 36
  • 73
1

Based on your other related question that was closed as a duplicate of this one (https://stackoverflow.com/questions/24996241/c-sharp-number-of-days-in-a-month-using-a-switch#24996339)...

This is clearly an academic exercise that wants you to learn about the switch statement.

Here is a complete example that demonstrates a couple ways to do switch statements. Since you already grabbed the month number from the user you can switch on that value by creating a mapping between the month and the number of days in the month.

To wit:

class MainClass
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Give me an integer between 1 and 12, and I will give you the month");

        int monthInteger = int.Parse(Console.ReadLine()); // WARNING: throws exception for non-integer input

        Console.WriteLine(GetMonthName(monthInteger));
        Console.WriteLine();

        Console.Write("Display days in month (y/n)? ");
        if (Console.ReadLine() == "y")
        {
            int daysInMonth = GetDaysInMonth_NoLeapYear(monthInteger);
            if (daysInMonth > 0)
            {
                Console.WriteLine(String.Format("{0} days in {1}",
                    daysInMonth.ToString(),
                    GetMonthName(monthInteger)));
            }
            else
            {
                Console.WriteLine("Invalid month entered.");
            }
            Console.WriteLine();
        }

        Console.WriteLine("Hit enter to close");
        Console.ReadLine();
    }

    private static String GetMonthName(int monthInteger)
    {
        DateTime newDate = new DateTime(DateTime.Now.Year, monthInteger, 1);
        String monthName = newDate.ToString("MMMM");
        return monthName;
    }

    /// <summary>
    /// Prints days in month. Assumes no leap year (since no year context provided) so Feb is always 28 days.
    /// </summary>
    /// <param name="monthInteger"></param>
    private static int GetDaysInMonth_NoLeapYear(int monthInteger)
    {
        int daysInMonth = -1; // -1 indicates unknown / bad value
        switch (monthInteger)
        {
            case 1: // jan
                daysInMonth = 30;
                break;
            case 2: // feb
                daysInMonth = 28; // if leap year it would be 29, but no way of indicating leap year per problem constraints
                break;
            case 3: // mar
                daysInMonth = 31;
                break;
            case 4: // apr
                daysInMonth = 30;
                break;
            case 5: // may
                daysInMonth = 31;
                break;
            case 6: // jun
                daysInMonth = 30;
                break;
            case 7: // jul
                daysInMonth = 31;
                break;
            case 8: // aug
                daysInMonth = 31;
                break;
            case 9: // sep
                daysInMonth = 30;
                break;
            case 10: // oct
                daysInMonth = 31;
                break;
            case 11: // nov
                daysInMonth = 30;
                break;
            case 12: // dec
                daysInMonth = 31;
                break;
        }
        return daysInMonth;
    }

    /// <summary>
    /// Prints days in month. Assumes no leap year (since no year context provided) so Feb is always 28 days.
    /// </summary>
    /// <param name="monthInteger"></param>
    private static int GetDaysInMonth_NoLeapYear_Compact(int monthInteger)
    {
        // uses case statement fall-through to avoid repeating yourself

        int daysInMonth = -1; // -1 indicates unknown / bad value
        switch (monthInteger)
        {
            case 2: // feb
                daysInMonth = 28; // if leap year it would be 29, but no way of indicating leap year per problem constraints
                break;
            case 3: // mar
            case 5: // may
            case 7: // jul
            case 8: // aug
            case 10: // oct
            case 12: // dec
                daysInMonth = 31;
                break;
            case 1: // jan
            case 4: // apr
            case 6: // jun
            case 9: // sep
            case 11: // nov
                daysInMonth = 30;
                break;
        }
        return daysInMonth;
    }
}

GetDaysInMonth_NoLeapYear_Compact is included only to illustrate the case fall-through behavior that lets multiple case statements go to the same code.

Community
  • 1
  • 1
nothingisnecessary
  • 6,099
  • 36
  • 60