-3

I'm creating program in which the user inputs a number that will display the month associated with that month (1 = january, 2 = feburary, 3 = march,....etc) however, when inputting the number and pressing enter, the program breaks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication27
{
    class Program
    {

        public static void Main()
        {
            int month = 0;

            //Range 1-12 refers to Month
            Console.WriteLine("Please enter Month in numerical increments (1-12)");
            month = int.Parse(Console.ReadLine());

            switch (month)
            {
                case 1:
                    Console.WriteLine("This is the First Month...January");
                    break;
                case 2:
                    Console.WriteLine("This is the Second Month...Februrary");
                    break;
                case 3:
                    Console.WriteLine("This is the Third Month...March");
                    break;
                case 4:
                    Console.WriteLine("This is the Fourth Month...April");
                    break;
                case 5:
                    Console.WriteLine("This is the Fifth Month...May");
                    break;
                case 6:
                    Console.WriteLine("This is the Sixth Month...June");
                    break;
                case 7:
                    Console.WriteLine("This is the Seventh Month...July");
                    break;
                case 8:
                    Console.WriteLine("This is the Eighth Month...August");
                    break;
                case 9:
                    Console.WriteLine("This is the Ninth Month...September");
                    break;
                case 10:
                    Console.WriteLine("This is the Tenth Month...October");
                    break;
                case 11:
                    Console.WriteLine("This is the Eleventh Month...November");
                    break;
                case 12:
                    Console.WriteLine("This is the Twelfth Month...December");
                    break;

                default:
                    Console.WriteLine("You have inputed an invalid Character");
                    break;
            }


            //Attempt to looP code

        }

    }
}

Unsure what is causing the termination

Ethan Bierlein
  • 3,353
  • 4
  • 28
  • 42
Bentus
  • 1
  • 3
  • Add a `Console.ReadLine()` at the very end? – SimpleVar Apr 26 '15 at 20:59
  • That wasn't part of the code just an accident when inputting the question. input and enter still ends the program prematurely. – Bentus Apr 26 '15 at 21:02
  • I'm assuming you're not getting any exception. So you NEED to add `Console.ReadLine()` at the end - this will prevent the console from automatically closing when debugging. – SimpleVar Apr 26 '15 at 21:05
  • I did attempt to add Console.ReadLine() , the program still cancels prematurely – Bentus Apr 26 '15 at 21:05
  • I presume you're using Visual Studio? From the Debug menu, select "Start without Debugging" and tell us what happens. – BJ Myers Apr 26 '15 at 21:15
  • You really get no exception? No error message? – SimpleVar Apr 26 '15 at 21:16
  • Visual Studio 2013, Start it with and without debug, same issue, no error messages. However i did move "Console.ReadLine()" before the break, and that atleast displays one of the lines, but still ends afterwards, instead of continuing – Bentus Apr 26 '15 at 21:19
  • 1
    Your program doesn't have any kind of loop in it. Once it gets to the end, it terminates. You have to add a `for` or `while` statement if you want it to keep going. – BJ Myers Apr 26 '15 at 21:22
  • Press F10 to step-by-step through your code. – SimpleVar Apr 26 '15 at 21:32

1 Answers1

1

Add a while loop to your code and set a code like "0" to exit the loop or terminate the program.

public static void Main()
    {
        int month = 0;
        bool exit = false;
        while (!exit)
        {
            //Range 1-12 refers to Month
            Console.WriteLine("Please enter Month in numerical increments (1-12)");
            month = int.Parse(Console.ReadLine());

            switch (month)
            {
                case 0:
                    exit = true;
                    break;
                case 1:
                    Console.WriteLine("This is the First Month...January");
                    break;
                case 2:
                    Console.WriteLine("This is the Second Month...Februrary");
                    break;
                case 3:
                    Console.WriteLine("This is the Third Month...March");
                    break;
                case 4:
                    Console.WriteLine("This is the Fourth Month...April");
                    break;
                case 5:
                    Console.WriteLine("This is the Fifth Month...May");
                    break;
                case 6:
                    Console.WriteLine("This is the Sixth Month...June");
                    break;
                case 7:
                    Console.WriteLine("This is the Seventh Month...July");
                    break;
                case 8:
                    Console.WriteLine("This is the Eighth Month...August");
                    break;
                case 9:
                    Console.WriteLine("This is the Ninth Month...September");
                    break;
                case 10:
                    Console.WriteLine("This is the Tenth Month...October");
                    break;
                case 11:
                    Console.WriteLine("This is the Eleventh Month...November");
                    break;
                case 12:
                    Console.WriteLine("This is the Twelfth Month...December");
                    break;

                default:
                    Console.WriteLine("You have inputed an invalid Character");
                    break;
            }
        }
        //Attempt to looP code

    }
Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19