1
if (testModetrue)
{
    try
    {
        Console.Write("What number do you want the roll to be set to? (1-6)");
        string diceString = Console.ReadLine();
        int diceCheck = int.Parse(diceString);
        if ((diceCheck >= minDiceValue) || (diceCheck <= maxDiceValue))
        {
            diceNo = int.Parse(diceString);
        }
        else if ((diceCheck <= minDiceValue) || (diceCheck >= maxDiceValue))
        {
            Console.WriteLine("Please enter a number between 1-6.");
            break;
        }
    }
    catch (Exception)
    {
        Console.WriteLine("An error has occured.");
        return;
    }
}

This code checks to see whether the answer given doesn't go past 6 or below 1, however whenever I run it, it does it anyway then it throws the out of array error, anybody help?

3
  • 1
    The one thing I have a problem with is that you use inclusive conditions (>= and <=) in both if and else if. Otherwise it would be nice to know where your code actually throws that exception. Commented Dec 9, 2013 at 18:48
  • 1
    There is no array in the code above, so I don't think that the exception is thrown here. Out of curiosity (and probably explaining the following exception) what is the value for minDiceValue and maxDiceValue Commented Dec 9, 2013 at 18:48
  • and better use tryparse Commented Dec 9, 2013 at 18:56

3 Answers 3

6
int diceCheck = int.Parse(diceString);
if ((diceCheck >= minDiceValue) || (diceCheck <= maxDiceValue))
{
    diceNo = int.Parse(diceString);
}

This conditional should be AND rather than OR. Also, since you're parsing the string before the conditional, you don't need to do it inside it, so you should change that part to:

int diceCheck = int.Parse(diceString);
if (diceCheck > maxDiceValue && diceCheck < minDiceValue)
{
   Console.Writeline("Please write a number between 1 and 6");
   break;
}

Your other if statement was also kind of redundant because you already have other variable (dicecheck) with the value, so remove it.

Sign up to request clarification or add additional context in comments.

3 Comments

It would be good to mention that that OP parses the string twice and he could do it only once.
and to mention that he should use a tryParse, in case the user enters a string.
Yeah that fixed it thanks and @BrunoCosta I need to parse it under two different names because if I did it under one name, it wouldn't work.
1
private const int maxDiceValue = 6;
private const int minDiceValue = 1;

Console.Write("What number do you want the roll to be set to? (1-6)");
string diceString = Console.ReadLine();
int diceCheck;

if (!int.TryParse(diceString, out diceCheck) ||
  diceCheck < minDiceValue ||
  diceCheck > maxDiceValue) {
  Console.WriteLine("Please enter a number between 1-6.");
  return;
}

// add diceCheck to array here

Comments

0

Lets imagine the user introduce -1.

In the first condition you are validating if -1 >= 1 which is false, but you are also validating if -1 <= 6 which is true.

Instead of && (AND ALSO) you are using || (Or Else).

Since one of the condition is always true, the validating will always return true and therefore the code will run throwing an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.