0

The user Input should be within 0 to 10 and I want to use tryparse and check if the input is correct, if not then it should prompt the user again. my code is like below it only works once but if the user enters the wrong number it again executes. In this case, where should I put the loop?


 int number;
            bool True;
            Console.Write("Enter number between 0 t0 10: ");
            True = int.TryParse(Console.ReadLine(), out number);
                while (number < 0 || number > 10)
                {
             while (True)
                    Console.Write("Enter number between 0 t0 10: ");
                    int.TryParse(Console.ReadLine(), out number);

                }

            {
                Console.WriteLine("Please enter the correct number");

            }
            return number;

1
  • 1
    You need to do something with the return value of both calls to TryParse - in case they type in Bob. Commented Jun 4, 2020 at 8:58

2 Answers 2

2

use do while .

int i = 0;

do
{
   Console.Write("Enter number between 0 t0 10: ");
   True = int.TryParse(Console.ReadLine(), out number);

} while (!True);
Sign up to request clarification or add additional context in comments.

1 Comment

int i = 0; do { Console.Write("Enter number between 0 t0 10: "); } while (!int.TryParse(Console.ReadLine(), out number)); i would suggest to remove the true variable as it is not used, for error handling use bahtiyars version.
1

I'd write similar code to what you already have, but I'd rename True to isValid and use a do/while loop:

int number;
bool isValid = false;
do
{
    Console.Write("Enter number between 0 and 10: ");

    isValid = int.TryParse(Console.ReadLine(), out number) 
                  && number >= 0
                  && number <= 10;

    if (isValid)
    {
        Console.WriteLine("Please enter the correct number");
    }
}
while (!isValid);

Better yet, do away with isValid altogether:

int number;
do
{
    Console.Write("Enter number between 0 and 10: ");
    if (int.TryParse(Console.ReadLine(), out number) && number >= 0 && number <= 10)
    {
        break;
    }
    Console.WriteLine("Please enter the correct number");
}
while (true);

Now, if the number is valid, we simply break out of the loop.

P.S. You'll see that I swaped the number conditions around to make them validity checks instead, so < became >= and > became <=.

Also, don't call a variable something like True. Imagine you're reading through a longer method and you come across this line (months after you've written it):

while (True)

That's an endless loop right? Oh no! It's the variable True, not the constant true. Now imagine further up you havebool True = false;. If you're reading quickly, you might misunderstand what the code does. Even if you're not, you have to put more mental effort into understanding what's happening.

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.