tags:

views:

158

answers:

4

Hey All :)

Well I was doing this simple code snippet to generate the Month name by using the Day Number! But I Got This Error.. Im not trying to change MonthName into int!

class Module04
    {
        public static void Exercise01()
        {
            Console.WriteLine("Please Enter a day number between 1 and 365: ");
            string line = Console.ReadLine();
            int dayNum = int.Parse(line);

            int monthNum = 0;

            if (dayNum <= 31) // January
            {
                goto End;
            }
            else
            {
                dayNum -= 31;
                monthNum++;
            }

            if (dayNum <= 28) // February
            {
                goto End;
            }
            else
            {
                dayNum -= 28;
                monthNum++;
            }

            if (dayNum <= 31) // March
            {
                goto End;
            }
            else
            {
                dayNum -= 31;
                monthNum++;
            }

            if (dayNum <= 30) // April
            { goto End; }
            else
            {
                dayNum -= 30;
                monthNum++;
            }

            if (dayNum <= 31) // May
            { goto End; }
            else
            {
                dayNum -= 31;
                monthNum++;
            }

            if (dayNum <= 30) // June
            { goto End; }
            else
            {
                dayNum -= 30;
                monthNum++;
            }

            if (dayNum <= 31) // July
            { goto End; }
            else
            {
                dayNum -= 31;
                monthNum++;
            }

            if (dayNum <= 31) // August
            { goto End; }
            else
            {
                dayNum -= 31;
                monthNum++;
            }

            if (dayNum <= 30) // September
            { goto End; }
            else
            {
                dayNum -= 30;
                monthNum++;
            }

            if (dayNum <= 31) // October
            { goto End; }
            else
            {
                dayNum -= 31;
                monthNum++;
            }

            if (dayNum <= 30) // November
            { goto End; }
            else
            {
                dayNum -= 30;
                monthNum++;
            }

            if (dayNum <= 31) // December
            { goto End; }
            else
            {
                dayNum -= 31;
                monthNum++;
            }

        End:

            string monthName;

            switch (monthName)
            {
                case 0 :
                    monthName = "January"; break;
                case 1 :
                    monthName = "February"; break;
                case 2 :
                    monthName = "March"; break;
                case 3 :
                    monthName = "April"; break;
                case 4 :
                    monthName = "May"; break;
                case 5 :
                    monthName = "June"; break;
                case 6 :
                    monthName = "July"; break;
                case 7 :
                    monthName = "August"; break;
                case 8 :
                    monthName = "September"; break;
                case 9 :
                    monthName = "October"; break;
                case 10 :
                    monthName = "November"; break;
                case 11 :
                    monthName = "December"; break;
                default :
                    monthName = "Not yet Done"; break;
            }
            Console.WriteLine("{0} {1}", dayNum, monthName);
        }
}
+2  A: 

It's the switch where you're using it as an int - You probably meant to pass monthNum to the switch statement.

(Also: Shouldn't you have monthNum == ... in all of the ifs in the first part?)

Jesse Millikan
+1  A: 

Change the switch to

switch (monthNum)
astander
Solved THankss :D
A: 

You are doing a switch on monthName, with int cases, thus c# try's to convert the string to int to match the cases.

http://msdn.microsoft.com/en-us/library/06tc147t(VS.80).aspx

Pim Jager
+3  A: 

monthName is a string so your switch cases should be a string also.

string monthName;

            switch (monthName)
            {
                case "0" :
                    monthName = "January"; break;
                case "1" :
                    monthName = "February"; break;
                case "2" :
                    monthName = "March"; break;
                case "3" :
                    monthName = "April"; break;
                case "4" :
                    monthName = "May"; break;
                case "5" :
                    monthName = "June"; break;
                case "6" :
                    monthName = "July"; break;
                case "7" :
                    monthName = "August"; break;
                case "8" :
                    monthName = "September"; break;
                case "9" :
                    monthName = "October"; break;
                case "10" :
                    monthName = "November"; break;
                case "11" :
                    monthName = "December"; break;
                default :
                    monthName = "Not yet Done"; break;
            }
        Console.WriteLine("{0} {1}", dayNum, monthName);
    }
RJ1516