2

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.
                    }
                }
            }
        }`
smyslov
  • 1,279
  • 1
  • 8
  • 29
PtempestC
  • 23
  • 5

1 Answers1

0

Try to declare List<string> list1 = new List<string>(); outside the first for loop, I think your problem is because you declare list1 each time, so at last iteration count of list1 is only 1, because each time you maked new one.

I think that is the problem.

On your position I would do something like this:

List<string> list1 = new List<string>(); 
for (int i = 0; i < gameLength; i++) // The code below will loop 10 times
{   
    string chosenWord = SelectWord(); // Selects a random word
    Console.Write(chosenWord + " "); // Prints the selected word
    list1.Add(chosenWord); // Adds the selected word to the list
}

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
}

int score = 0;
for (int t = 0; t < 10; t++)
{
    if (list1[t] == list2[t]) // Now should work
    { 
        score++;        
    }
}

Console.WriteLine(score);
David
  • 38
  • 7
  • Maybe just one comment. When you compare strings you should be careful, because String is and object. I would recommend you to use `.Equals()` – David Jul 15 '15 at 11:56