Hi i have wrote a guessing game program but i am get an error when i play it i can get to 99 guesses then i get array out of bound exception error

i think my problem is in the fill array method but i could be wrong.

could some one please have a look for me

my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PickLarger
{
    public partial class Form1 : Form
    {
        int[] array1 = new int[100];// making an array of 100
        int[] array2 = new int[100];
        Random ranNum = new Random();
        int correctGuess = 0;
        int incorrectGuess = 0;
        public Form1()
        {
            InitializeComponent();
            fillArray();// Call fill array method to start when form loads
        }

        class click
        {
            public static int buttonClick = 0;//  so i can count the button clicks in both loops at the same time   
        }

        private void fillArray()
        {
            for (int i = 0; i < 100; i++)
            {
                this.array1[i] = ranNum.Next(0, 100);
                System.Threading.Thread.Sleep(1);
                this.array2[i] = ranNum.Next(0, 100);
            }
        }
        private void btn1_Click(object sender, EventArgs e)
        {
            click.buttonClick++;// Counting button clicks
            int i = click.buttonClick;
            if (i < 101)
            {
                if (array1[i] > array2[i])
                {
                    lblCorrectGuess.Visible = true;
                    lblCorrectGuess.Text = "You win button 1 equaled :  " + array1[i].ToString() + "  Button 2's random Number was :  " + array2[i].ToString();
                    correctGuess++;
                    BackColor = Color.Green;
                    lblWrongGuess.Visible = false;
                }
                else
                {
                    lblWrongGuess.Visible = true;
                    lblWrongGuess.Text = "You Lose button 2 equaled : " + array2[i].ToString() + "   Button 1's random number was :  " + array1[i].ToString();
                    incorrectGuess++;
                    BackColor = Color.Red;
                    lblCorrectGuess.Visible = false;
                }
                i++;
                btn1.Enabled = false;
                btn2.Enabled = false;
                lblGuessCount.Text = "You have made  :  " + correctGuess + "  Correct guesses  You have also guesed incorrectly : "+ incorrectGuess+" times";
              
            }
            else
            {
                Form2 form2 = new Form2();
                form2.Show();
                click.buttonClick = 0;
                this.Hide();
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            click.buttonClick++;// Counting button clicks
            int i = click.buttonClick;
            if (i < 101)// run loop for 4 times
            {
                if (array2[i] > array1[i])
                {
                    lblCorrectGuess.Visible = true;
                    lblCorrectGuess.Text = "You win button 2 equaled :  " + array2[i].ToString() + "  Button 1's random Number was" + array1[i].ToString();
                    correctGuess++;
                    BackColor = Color.Green;
                    lblWrongGuess.Visible = false;
                }
                else
                {
                    lblWrongGuess.Visible = true;
                    lblWrongGuess.Text = "You Lose button 1 equaled : " + array1[i].ToString() + "  Button 2's random number was : " + array2[i].ToString();
                    incorrectGuess++;
                    BackColor = Color.Red;
                    lblCorrectGuess.Visible = false;
                }
                i++;
                btn2.Enabled = false;
                btn1.Enabled = false;
                lblGuessCount.Text = "You have made  :  " + correctGuess + "  Correct guesses  You have also guesed incorrectly : " + incorrectGuess + " times";
            }
            else
            {
                Form2 form2 = new Form2();
                form2.Show();
                click.buttonClick = 0;
                this.Hide();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            btn1.Enabled = true;
            btn2.Enabled = true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.Show();
            this.Hide();
        }
    }
}

Edited 5 Years Ago by m_wylie85: n/a

Line 80 contains

if (i < 101)// run loop for 4 times

Where it should be

if (i < 100)// run loop for 4 times

Remember, the array index starts at 0 and finishes at 99 (if the array size is specified at 100), so array index 100 is out of bounds.

This question has already been answered. Start a new discussion instead.