dimanche 21 janvier 2018

"Index was out of range" - can't figure out why

I need help for a school task I've been working on. I am making a program that creates statistics of a football championship based on different conditions. I've got a text file with the rounds, the results, the halftime results and the teams that played, all divided by spaces (Eg. 12 2 0 1 0 Clevers Dumbs). I read the file and divided the lines into sections (rounds became matches[].round, final home goals became matches[].awayGoalEnd, etc.). Everything worked fine, until I got the task to ask for a team name (inputTeam) and look up in which round did the team first lose at home (if the team is on the left in the text file, then they played at home) and from which team.

I got everything working in case a team has lost at home, but when I try with a team that has not lost at home, I get the following error for my if statement: System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' And I just can't find why...

    Console.WriteLine("Give me a team name: ");
    string inputTeam = Console.ReadLine();

    bool nolose = true;
        int i = 1;
        do
        {
            if (matches[i].homeTeam == inputTeam && mathces[i].homeGoalEnd < matches[i].awayGoalEnd) //here's where I got the error
            {
               nolose = false;
               Console.WriteLine($"The team first lose at home in round {matches[i].round} from the team {matches[i].awayTeam}");
            }
            i++;
        } while (nolose || i > matches);

        if (nolose)
        {
            Console.WriteLine("The team didn't lose any matches at home.");
        }

Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire