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..
5 Answers 5
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();
Comments
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
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
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
`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;`
List<string>(andVariableName.Add) rather than an array.for (int i = 1; i <=2 ; i++)should befor (int i = 0; i < 2 ; i++).