user2908363
user2908363

Reputation: 17

how to re-display user prompt after invalid input is given in c#?

I am working on a program that functions like a quiz. Although I am having issues right now with not allowing the user to simply hit 'Enter' without typing in a guess and proceeding to the next question. I am trying two different ways of doing it, the first way is using a try and catch block, but I am having issues understanding how that works exactly. I have tried looking up the correct syntax for try and catch blocks and tried including breaks and continue within, though I would get an error message that read "No enclosing loop out of which to break or continue"

public void Play()
    {
        DisplayWelcome();

        Console.WriteLine("First Question:");
        Console.WriteLine("---------------");
        Console.WriteLine(entertainmentquestions[0]);
        guess = Console.ReadLine();
        try
        {
            if (guess == entertainmentanswers[0])
            {
                Console.WriteLine("You got it right!");
                Console.WriteLine("Press 'Enter' when you are ready to see the next question");
                Console.ReadLine();
                Console.Clear();
            }

            else
            {
                Console.WriteLine("You got this one wrong! Better luck next one");
                Console.WriteLine("Press 'Enter' when you are ready to see the next question");
                Console.ReadLine();
                Console.Clear();
            }
            break;
        }

        catch 
        {
            Console.WriteLine("Incorrect input, please enter a valid answer");
            continue;
        }

The second way I've thought it could be done would be through an if statement but I don't know how I could re-prompt the user after their hitting enter has been displayed as 'invalid'

 public void Play()
    {
        DisplayWelcome();
        Console.WriteLine();
        Console.WriteLine("First Question:");
        Console.WriteLine("---------------");
        Console.WriteLine(hstryquestions[0]);
        guess = Console.ReadLine().ToUpper();
        if (guess != "")
        {
            if (guess == hstryanswers[0])
            {
                Console.WriteLine();
                Console.WriteLine("You got it right!");
                Console.WriteLine("Press 'Enter' when you are ready to see the next question");
                Console.ReadLine();
                Console.Clear();
            }

            else
            {
                Console.WriteLine();
                Console.WriteLine("You got this one wrong! Better luck next one");
                Console.WriteLine("Press 'Enter' when you are ready to see the next question");
                Console.ReadLine();
                Console.Clear();
            }
        }
        else if (guess == "")
        {
            Console.WriteLine("Please enter a valid answer");
            Console.ReadLine();

        }

Any help would be greatly appreciated, I am struggling with finding the answer to this online and in the C# books I have. Perhaps is could be I just don't know what exactly to look for..

Upvotes: 1

Views: 2750

Answers (1)

AndyG
AndyG

Reputation: 41120

Trying using a loop! Perhaps a do-while loop!

Something like this:

do{
ask for input
get input
}while(input is not valid)

This kind of loop will repeatedly get user input until the input passes some validity test that you define. Here's a tutorial on do while loops in C#: http://www.dotnetperls.com/do

Upvotes: 1

Related Questions