0

Hoping someone can help. I am teaching myself C# and one of the challenges in this chapter has tasked me with storing the number of days in each month in an array which I called daysInMonth. When the program starts, I am to ask for a user to input a number between 1 and 12 and then spit out the number of days in the month that corresponds with that number.

I have searched for this but am coming up with nothing. Most examples are related to matching/finding an int or string with something in the array which isn't what I want. I want something so that if the user enters the number 5, the program will print out whatever is in the 5th whatever in the array. I know this is pretty easy but I think that my searching is coming up with nothing because I don't know the correct term to search for. Any help would be appreciated.

UPDATE:

Thanks to MAV I got it working. Posting full code of the program.

        int[] daysInMonth = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        string[] monthNames = new string[12] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        int myChoice;

        Console.Write("Please enter a number: ");

        myChoice = Int32.Parse(Console.ReadLine());

        if (myChoice < 1)
        {
            Console.WriteLine("Sorry, the number {0} is too low.  Please select a number between 1 and 12.", myChoice);
            Console.Write("Please enter a number: ");
            myChoice = Int32.Parse(Console.ReadLine());
        }
        else if (myChoice > 12)
        {
            Console.WriteLine("Sorry, the number {0} is too high.  Please select a number between 1 and 12.", myChoice);
            Console.Write("Please enter a number: ");
            myChoice = Int32.Parse(Console.ReadLine());
        }

        int i = daysInMonth[myChoice - 1];
        string m = monthNames[myChoice - 1];

        Console.WriteLine("Thank you.  You entered the number {0}.", myChoice);
        Console.WriteLine("That number corresponds with the month of {0}.", m);
        Console.WriteLine("There are {0} days in this month.", i);

        Console.ReadLine();
1
  • please provide the code you have, then someone might be able to advise how to fix it. Commented Jun 13, 2012 at 23:15

1 Answer 1

4

Since you want to learn C# I am not going to give you what I believe is the answer. Instead I will try to give you knowledge about how to use arrays, since that seems to be your problem.

You can declare arrays like this:

 int[] intArray = {1, 2, 3};      //This array contains 1, 2 and 3
 int[] intArray2 = new int[12];   //This array have 12 spots you can fill with values
 intArray2[2] = 42;               //element 2 in intArray2 now contains the value 42

To access an element in an array, you can do this:

int i = intArray2[2];             //Integer i now contains the value 42.

For more information about arrays and how to use them I can recommend reading this tutorial: Arrays Tutorial

Sign up to request clarification or add additional context in comments.

1 Comment

That was really good, thank you so much. That gave me enough info to figure it out for myself. I did appreciate that too. You get that warm fuzzy feeling when you get it to work yourself. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.