-2

I'm writing a Assignment for school (we are sepose to make one for the rest of the class) and I have run in to a problem. I'm having IndexOutOfRangeException errors when running the program and I can't figure out why I'm getting it.

        Random rnd = new Random();
        string strNumDice = tbxNumDice.Text;
        int[] numDice = new int[int.Parse(strNumDice)];
        int numSides = int.Parse(tbxNumSides.Text);
        int trgNumber = int.Parse(tbxTarget.Text);
        int sum = 0;
        //int numTries = 0;
        int var2 = 0;

        if (int.Parse(strNumDice) * (numSides) >= trgNumber)
        {
            while (var2 != trgNumber)
            {
                tbxResult.AppendText("\n");
                sum = 0;
                foreach (int numberOfDice in numDice)
                {
                    numDice[numberOfDice] = rnd.Next(1, numSides+1);
                    tbxResult.AppendText(numDice[numberOfDice].ToString() + " ");
                    sum += numDice[numberOfDice];
                    //numTries++;
                }
                tbxResult.AppendText("\n");
                tbxResult.AppendText("The new sum is" + sum.ToString());
                var2 = sum;
            }
        }

If I decide to roll 6 d6's and wan't a target sum of 36 it will continue to do so until it gets to 36.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
O. Dolf
  • 1
  • 1
  • put validation on input :) – Kamran Shahid Jan 28 '19 at 14:35
  • 1
    can you try changing "foreach (int numberOfDice in numDice)" into "for (int numberOfDice = 0; numberOfDice < numDice.length; numberOfDice++) {...} – Eray Balkanli Jan 28 '19 at 14:36
  • 2
    You are using the dice rolls as indexes, this: `foreach (int numberOfDice in numDice)` will not loop from 0 to 5 if you specified 6 dice, it will loop over the existing dice rolls. If one of those dice rolls is 6, then you have an index out of range exception when you try to access the 6-element array. Try this instead: `for (int numberOfDice = 0; numberOfDice < numDice.Length; numberOfDice++)`. – Lasse V. Karlsen Jan 28 '19 at 14:38
  • Try following : var2 <= trgNumber – jdweng Jan 28 '19 at 14:38
  • 1
    Essentially, the first time your loop is executed it will do this: `numDice[0] = random; numDice[0] = random; numDice[0] = random; numDice[0] = random; numDice[0] = random; numDice[0] = random;` Let's say the last random number there was 5, then the next time it will do `numDice[5] = random; numDice[0] = random; numDice[0] = random; numDice[0] = random; numDice[0] = random; numDice[0] = random;`. If at any point the rolled dice hits a 6, the next time around it will try to do `numDice[6] = random;` and there's your IndexOutOfRangeException. – Lasse V. Karlsen Jan 28 '19 at 14:45

1 Answers1

0

The mistake is here:

foreach (int numberOfDice in numDice)
{
    numDice[numberOfDice] = rnd.Next(1, numSides+1);

This is how javascript does foreach loops: you get the indexes of your array. C# gives you the values of the actual elements, rather than the indexes. As you will be modifying actual array elements, you want a for loop, rather than a foreach loop, like this:

for(int i = 0; i<numDice.Length; i++)
{
     numDice[i] = rnd.Next(1, numSides+1);
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794