I'm currently trying to create a Typing Test program. I'm having issues comparing the elements inside these two lists list1 (10 random words) and list2 (10 user inputs). Here is the error message that I get: ArgumentOutOfRangeException was unhandled
.
I'm not sure why but when I go into the debug menu, It shows list1
's value as Count = 1
, but then list2
's value as Count = 10
. All of the elements in both lists are strings. So my question is how do I compare the elements in these lists in order (first element of the first list with the first element of the second list) and so on.
I'm relatively new to coding, I don't understand why the code below isn't working. I've been trying to fix this for a few hours so thank you in advance for any help!
`for (int i = 0; i < gameLength; i++) // The code below will loop 10 times
{
List<string> list1 = new List<string>();
string chosenWord = SelectWord(); // Selects a random word
Console.Write(chosenWord + " "); // Prints the selected word
list1.Add(chosenWord); // Adds the selected word to the list
if (i == 9) // True once above code has been performed 10 times
{
Console.WriteLine();
List<string> list2 = new List<string>();
for (int b = 0; b < 10; b++) // This will also loop 10 times
{
string userValue = UserInput(); // Gets user input
list2.Add(userValue); // Adds user value to list
}
for (int t = 0; t < 10; t++)
{
if (list1[t] == list2[t]) // Here is the error
{
score++;
Console.WriteLine(score);
/* The error occurs on the second pass
* when the value of t becomes 1, But i don't
*/ understand why that doesn't work.
}
}
}
}`