Validating User Input? C#

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2009-12-05T22:42:30Z Indexed on 2010/03/26 13:43 UTC
Read the original article Hit count: 410

Filed under:
|
|

Hi, in an assignment, I have designed a input validation loop in C#, and I would like it to be able to check for the correct input format. I'm not for sure, but I think my designed loop is not checking the type of input, just what char is entered. I know I could use a try-catch block, but shouldn't you only use exceptions for exceptional situations? This is not an exceptional situation, because I expect that the user would enter an incorrect value.

Input validation is not part of my assignment, so the loop is in a homework assignment, but is not part of the homework assignment.

Question:

Is there a way I could redesign this loop so that it checks for valid input type as well?

Code:

do
    {
        Console.Write("Do you wish to enter another complex number?: (Y or N)");
        response = char.Parse(Console.ReadLine());
        response = char.ToUpper(response);

        if (response != 'Y' && response != 'N')
            Console.WriteLine("You must respond Y or N!");

    } while (response != 'Y' && response != 'N');

Thanks!!

© Stack Overflow or respective owner

Related posts about c#

Related posts about input-validation