1
    string Input = "";
    string[] Words = { "elephant", "lion" };
    string[] Clues = { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?"};

.........

        Console.WriteLine("Do you want to add you own animal? y/n ? \n");
        Input = Console.ReadLine();
        if (Input == "Y" || Input == "y")
        {
            Console.WriteLine("Enter an animal name: \n");
            //Array.Resize(ref Words, Words.Length + 1);
            Input = Console.ReadLine();
            Words[Words.Length] = Input;


            Console.WriteLine("Enter 2 clues \n");
            for (int i = 1; i <=2 ; i++)
            {
                Console.WriteLine("Clue" + i + ":");
                Clues[Clues.Length] = Console.ReadLine();

            }

        }

This is the standard guess the animal game.. I am getting an index out of bounds at line Words[Words.Length] = Input;.. the new animal and clues entered also needs to be available the next time i play the game..

3
  • 3
    Use List<string> (and VariableName.Add) rather than an array. Commented Dec 11, 2017 at 6:21
  • Possible duplicate of How to properly save application data for later use Commented Dec 11, 2017 at 6:22
  • Also, arrays in C# are 0-based, not 1-based. So for (int i = 1; i <=2 ; i++) should be for (int i = 0; i < 2 ; i++). Commented Dec 11, 2017 at 6:32

5 Answers 5

2

Instead string [] use List<T> from System.Collections.Generic

And you can use Add method to add new value like this.

Console.WriteLine("Enter an animal name: \n");
Input = Console.ReadLine();
Words.Add(Input);

And if want an array at the end, you can use ToArray method. Like this.

Words.ToArray();
Sign up to request clarification or add additional context in comments.

Comments

0

You can use List<string> instead of Array. Your code throw exception because you are trying to add element to an Array in outranged index. I modified your code like this;

        string Input = "";
        var Words = new List<string> { "elephant", "lion" };
        var Clues = new List<string> { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?" };
        Console.WriteLine("Do you want to add you own animal? y/n ? \n");
        Input = Console.ReadLine();
        if (Input == "Y" || Input == "y")
        {
            Console.WriteLine("Enter an animal name: \n");
            //Array.Resize(ref Words, Words.Length + 1);
            Input = Console.ReadLine();
            Words.Add(Input);


            Console.WriteLine("Enter 2 clues \n");
            for (int i = 1; i <= 2; i++)
            {
                Console.WriteLine("Clue" + i + ":");
                var clueInput = Console.ReadLine();
                Clues.Add(clueInput);

            }

        }

Comments

0

Adding a string to a finite array such as the one you have declared causes this additional item to be beyond the bounds of this array, ie. you are squeezing more things into a space that cannot hold that amount. Once created (at compilation) this size is fixed and can't be changed until next time.

I think what you are looking for are lists List<string> that use list.Add() to easily manipulate content dynamically during runtime.

Let me know if that answers your question or if you require more details.

Comments

0

why don't you use List rather Array.

     string Input = "";
    List<string> Words = new List<string>(){ "elephant", "lion" };
    List<string>  Clues =new List<string>() { "Has trunk?", "Is gray?", "Is yellow?", "Has mane?"};

 Console.WriteLine("Do you want to add you own animal? y/n ? \n");
    Input = Console.ReadLine();
    if (Input.toLower() == "y")
    {
        Console.WriteLine("Enter an animal name: \n");
        //Array.Resize(ref Words, Words.Length + 1);
        Input = Console.ReadLine();
        Words.Add(Input);


        Console.WriteLine("Enter 2 clues \n");
        for (int i = 1; i <=2 ; i++)
        {
            Console.WriteLine("Clue" + i + ":");
            Clues.Add(Console.ReadLine());

        }

    }

please follow this.

Comments

0
`Console.WriteLine("Enter an animal name: \n");
//Hear Words length is 2 and Data (0 = elephant,1 = lion)
Array.Resize(ref Words, Words.Length + 1);
//Hear Words length is 3 and Data (0 = elephant,1 = lion,2 = null)
Input = Console.ReadLine();
//if you write Words[Words.Length] means you tried to access 3 index which is not available.
//You should write this.
Words[Words.Length - 1] = Input;`

Comments

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.