0

I have a method here that allows the user to enter any number except for 999. But I don't know how to create a validation when user enters a negative number or letter.

    static int EnterScores(double[] scores, int maxScores)
    {
        double userInput;           
        int count = 0;
        while (count < maxScores)
        {
            Console.Write("Enter a score(or 999 to quit): ");
            userInput = double.Parse(Console.ReadLine());
            if (userInput == 999 || userInput < 0)
                break;
            scores[count++] = userInput;       
        }
        return count;
    }
SH093
  • 27
  • 6
  • 2
    You already have a check with `userInput < 0`. What is the problem you have? – Progman Apr 09 '19 at 20:19
  • Sorry I should've said creating a error message for whenever user inputs negative number or a letter – SH093 Apr 09 '19 at 20:22
  • When `maxScores` is larger than the array's length you'll get an index out of range exception. It also seems arbitrary, that the user can enter `1000` or larger but `999` ends it. – sticky bit Apr 09 '19 at 20:31
  • see https://stackoverflow.com/questions/7607260/check-non-numeric-characters-in-string for how to check for non-numeric characters. Once you have a string with no non-numeric characters, you can check for <999. – Frank Ball Apr 09 '19 at 20:55

3 Answers3

1

You can use double.TryParse. If it can convert string to float - it will be true

...
var str = Console.ReadLine();
if (double.TryParse(str, out userInput)){
    if (userInput == 999 || userInput < 0)
        break;
    scores[count++] = userInput;
}
...
vik_78
  • 1,107
  • 2
  • 13
  • 20
  • I tried that before. The problem is that the Console.Write("Enter a score(or 999 to quit): "); has to read continuously. With the code, you linked I have to press enter to input. – SH093 Apr 09 '19 at 20:32
  • @SH093 try add `else` to this `if`. Like `if (double.TryParse(...)) { ... } else { break; }` – Aleks Andreev Apr 09 '19 at 20:39
  • ok, that fixes the first problem. My main issue is still giving me an error whenever I input a letter. – SH093 Apr 09 '19 at 20:46
0

Separate the tests for userInput == 999 and userInput < 0, like this:

...
if (userInput == 999)
{
    break;
}
else if (userInput < 0)
{
    Console.WriteLine("Invalid input");
} 
else
{
    scores[count++] = userInput;
}
...
RWRkeSBZ
  • 723
  • 4
  • 11
  • 1
    This works but then what about the letter validation? – SH093 Apr 09 '19 at 20:35
  • In this case, use `double.TryParse` as suggested by @vic_78 in his answer. If the result is `false`, it means the input was not a value that can be converted to a double, in which case you can display the "invalid input" error message. – RWRkeSBZ Apr 09 '19 at 20:39
0

If user inputs a letter or something that can't be converted to double your this piece of code double.TryParse will throw an exception and program will fail.

So you should use try-catch block here:


try
{
    userInput = double.Parse(Console.ReadLine());
    if (userInput == 999)
    {
        break;
    }
    else if (userInput < 0)
    {
        Console.WriteLine("Invalid input");
    } 
    else
    {
        scores[count++] = userInput;
    }
}
catch(Exception e)  // will take care of letters
{
    Console.WriteLine("You enter an Invalid input !"):
}
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26