user1820059
user1820059

Reputation: 21

Having trouble with code to convert number into a month

here is my source code, everything is good until i get to the output and I cant get this to work. Visual Studio doesn't like what I have in the output section, labeled //OUTPUT.

What do I need to add or change to get this to work?

static void Main(string[] args)
{
    int monthNumber;
    string monthName;

    //INPUT
    Console.WriteLine("Please enter the number of the month");
    monthNumber = Convert.ToInt16(Console.ReadLine());

    //PROCCESSESS
    if (monthNumber == 1)
    {
        monthName = "January";
    }
    else if (monthNumber == 2)
    {
        monthName = "February";
    }
    else if (monthNumber == 3)
    {
        monthName = "March";
    }
    else if (monthNumber == 4)
    {
        monthName = "April";
    } 
    else if (monthNumber == 5)
    {
        monthName = "May";
    }
    else if (monthNumber == 6)
    {
        monthName = "June";
    }
    else if (monthNumber == 7)
    {
        monthName = "July";
    }
    else if (monthNumber == 8)
    {
        monthName = "August";
    }
    else if (monthNumber == 9)
    {
        monthName = "September";
    }
    else if (monthNumber == 10)
    {
        monthName = "October";
    } 
    else if (monthNumber == 11)
    {
        monthName = "November";
    }
    else if (monthNumber == 12)
    {
        monthName = "December";
    }

    //space to increase readability
    Console.WriteLine(Environment.NewLine);

    //OUTPUT
    Console.WriteLine("Month:" + monthName);
    Console.ReadLine();
}

Upvotes: 0

Views: 653

Answers (3)

Gustav Klimt
Gustav Klimt

Reputation: 440

  • a) declare a variable
  • b) if you are using month names, and you want some specific names, try using enum. really eazy and functional.

You can make your own setup of names/markings with your own pace of numbering those names/markings.

For Eg.

enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

Upvotes: 0

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 62002

You have a lot of else if, but in the end, there's no else to cover the case where none of the if apply. Therefore the compiler can't guarantee that monthName was ever assigned. Maybe the user typed "28"?

It would look better to use a switch statement with twelve case sections and one default section.

But also, the month names are built into the framework. So with using System.Globalization; you could simply say

monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNumber);

or

monthName = CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(monthNumber);

You can also get a DateTime directly from the input:

DateTime dateTime = DateTime.ParseExact(Console.ReadLine(), "%M", null);

Then

monthName = dateTime.ToString("MMMM");

Upvotes: 4

Sina Iravanian
Sina Iravanian

Reputation: 16296

monthName must be initialized before use. So you can change the declaration line as

string monthName = null;

That's because the code does not guarantee that monthName gets assigned. For example what if the input number is 13?

Upvotes: 4

Related Questions