Skip to main content
deleted 1 character in body
Source Link
Quill
  • 11.9k
  • 5
  • 40
  • 93

That's a pretty good start...

Be aware that entering text (like "hello") at a number prompt will cause an exception. ThereThere are several ways to approach this. TheThe easiest would be to use Int32.TryParseInt32.TryParse. HereHere is an example:

    static int PromptForInteger(string message, int minValue, int maxValue)
    {
        bool done;
        int answer = 0;

        // it will run at least once, but we don't know how many times
        do
        {
            // start with an assumption
            done = true;

            // display the prompt
            Console.Write(message);

            // check to see if what was entered "looks like" an integer
            if (Int32.TryParse(Console.ReadLine(), out answer) == false)
            {
                // display an error message, and try again
                Console.WriteLine("Could not convert your input into a whole number, please try again");
                // set our sentinel to indicate a problem
                done = false;
            } else if (answer < minValue || answer > maxValue)
            // if the value within the acceptible range?
            {
                // display an error message, and try again
                Console.WriteLine("Valid values are between {0} and {1}, please try again", minValue, maxValue);
                // set our sentinel to indicate a problem
                done = false;
            }
        } while (!done);

        // if we make it here, everything is OK
        return answer;
    }

That's a pretty good start...

Be aware that entering text (like "hello") at a number prompt will cause an exception. There are several ways to approach this. The easiest would be to use Int32.TryParse. Here is an example:

    static int PromptForInteger(string message, int minValue, int maxValue)
    {
        bool done;
        int answer = 0;

        // it will run at least once, but we don't know how many times
        do
        {
            // start with an assumption
            done = true;

            // display the prompt
            Console.Write(message);

            // check to see if what was entered "looks like" an integer
            if (Int32.TryParse(Console.ReadLine(), out answer) == false)
            {
                // display an error message, and try again
                Console.WriteLine("Could not convert your input into a whole number, please try again");
                // set our sentinel to indicate a problem
                done = false;
            } else if (answer < minValue || answer > maxValue)
            // if the value within the acceptible range?
            {
                // display an error message, and try again
                Console.WriteLine("Valid values are between {0} and {1}, please try again", minValue, maxValue);
                // set our sentinel to indicate a problem
                done = false;
            }
        } while (!done);

        // if we make it here, everything is OK
        return answer;
    }

That's a pretty good start...

Be aware that entering text (like "hello") at a number prompt will cause an exception. There are several ways to approach this. The easiest would be to use Int32.TryParse. Here is an example:

    static int PromptForInteger(string message, int minValue, int maxValue)
    {
        bool done;
        int answer = 0;

        // it will run at least once, but we don't know how many times
        do
        {
            // start with an assumption
            done = true;

            // display the prompt
            Console.Write(message);

            // check to see if what was entered "looks like" an integer
            if (Int32.TryParse(Console.ReadLine(), out answer) == false)
            {
                // display an error message, and try again
                Console.WriteLine("Could not convert your input into a whole number, please try again");
                // set our sentinel to indicate a problem
                done = false;
            } else if (answer < minValue || answer > maxValue)
            // if the value within the acceptible range?
            {
                // display an error message, and try again
                Console.WriteLine("Valid values are between {0} and {1}, please try again", minValue, maxValue);
                // set our sentinel to indicate a problem
                done = false;
            }
        } while (!done);

        // if we make it here, everything is OK
        return answer;
    }
Source Link
egray
  • 191
  • 2

That's a pretty good start...

Be aware that entering text (like "hello") at a number prompt will cause an exception. There are several ways to approach this. The easiest would be to use Int32.TryParse. Here is an example:

    static int PromptForInteger(string message, int minValue, int maxValue)
    {
        bool done;
        int answer = 0;

        // it will run at least once, but we don't know how many times
        do
        {
            // start with an assumption
            done = true;

            // display the prompt
            Console.Write(message);

            // check to see if what was entered "looks like" an integer
            if (Int32.TryParse(Console.ReadLine(), out answer) == false)
            {
                // display an error message, and try again
                Console.WriteLine("Could not convert your input into a whole number, please try again");
                // set our sentinel to indicate a problem
                done = false;
            } else if (answer < minValue || answer > maxValue)
            // if the value within the acceptible range?
            {
                // display an error message, and try again
                Console.WriteLine("Valid values are between {0} and {1}, please try again", minValue, maxValue);
                // set our sentinel to indicate a problem
                done = false;
            }
        } while (!done);

        // if we make it here, everything is OK
        return answer;
    }