-1

This code currently handles inputs of numbers just fine whether greater or less or within the accepted range, however, the program crashes when anything that isn't a number is entered with an unhandled exception error.System.FormatException: 'Input string was not in a correct format.' That's the error if that helps at all.

using System;

class Program
{


    static void PrintHello(int NumberOfTimes)
    {
        if (NumberOfTimes > 1 && NumberOfTimes < 11)

        {
            for (int Counter = 0; Counter < NumberOfTimes; Counter++)
            {
                Console.WriteLine("Hello.");
            }
        }
        else { Console.WriteLine("Error, please enter a number between 1 and 10"); }

    }

    static void Main(string[] args)
    {
        int myNumber = 1;
        while (myNumber != 0)
        {
            Console.WriteLine("Enter a number between 1 and 10, or 0 to stop");
            myNumber = Convert.ToInt16(Console.ReadLine());
            PrintHello(myNumber);
        }

    }
}
IsaacGrey
  • 3
  • 1
  • 1
    Instead of using `Convert.ToInt` you can use `TryParse` and then check if the parsing succeeded. https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx – stephen.vakil Oct 27 '17 at 18:45
  • Have you tried Google? Have you even used the search function on stack overflow? This has a very simple answer that is covered in tons of places. One of the most useful things to learn in program is researching a problem – Dave Oct 27 '17 at 18:46
  • Possible duplicate of [How can I validate console input as integers?](https://stackoverflow.com/questions/4804968/how-can-i-validate-console-input-as-integers) – Dave Oct 27 '17 at 18:48

2 Answers2

3

Just replace your Convert.ToInt16(...) with the following:

var input = Console.ReadLine();
if (int.TryParse(input, out myNumber))
{
     PrintHello(myNumber);
}

The int.TryParse-method will return true, if the given input can be parsed as an integer, otherwise false. If it could parse the input, the value will be stored in the second parameter (which is flagged as out.

Edit: As a rule of thumb: Never parse user-input directly (especially when you are handling with strings as input). Check if the input is valid.

Link
  • 1,307
  • 1
  • 11
  • 23
-1

You can use try and catch when parsing the input string as shown here:

try
{
  int NumberOfTimes = int.Parse(Console.ReadLine());
}
catch(System.FormatException)
{
  Console.WriteLine("Incorrect format of the user's input");
}
Aurelius Prochazka
  • 4,510
  • 2
  • 11
  • 34
  • 1
    Why use Exceptions instead of `TryParse`? – Steve Oct 27 '17 at 19:26
  • Don't use Exception-Handling as control-flow! See https://stackoverflow.com/questions/3259660/example-of-using-exceptions-to-control-flow – Link Oct 27 '17 at 19:28
  • While not the *best* method of doing this, it *does* work, and handling invalid user input could (arguably) be considered an "exceptional" situation, worthy of a try-catch block. – Bradley Uffner Oct 27 '17 at 20:08