none
Using a loop when you have used the user input to create different outcomes. RRS feed

  • Question

  • Hello,

    So I am coding c sharp and I have got my code to say that if the user enter either 1,2 or 3 a different outcome happens but then I realised that what happens if the user enters a different number to 1,2 or 3...

    So I want to know how I can create a loop for my command until the user inputs the correct number?

    Thanks

    Console.Write("What is your name: ");
                string name = Console.ReadLine();
                Console.WriteLine("Welcome " + name + " to the Spelling Game");
                Console.WriteLine("Enter 1 for Beginner");
                Console.WriteLine("Enter 2 for Intermediate");
                Console.WriteLine("Enter 3 for Advanced");
                int result;
                if (int.TryParse(Console.ReadLine(), out result))
                 {
                    if (result == 1)
                    Console.WriteLine("Welcome to Beginner, " + name);
                    else if (result == 2)
                        Console.WriteLine("Welcome to Intermediate, " + name);
                    else if (result == 3)
                        Console.WriteLine("Welcome to Advanced, " + name);
                    else
                        Console.WriteLine("Incorrect Response");

    Sunday, October 18, 2020 3:36 PM

Answers

  • There are different ways in which you can write the loop, but one of them is to define a boolean variable called "success" and loop until success is true. Of course, you initialize it to false and you set it to true in the cases where a correct number is entered:

    Console.Write("What is your name: ");
                string name = Console.ReadLine();
    
    bool success = false;
    do
    {
                Console.WriteLine("Welcome " + name + " to the Spelling Game");
                Console.WriteLine("Enter 1 for Beginner");
                Console.WriteLine("Enter 2 for Intermediate");
                Console.WriteLine("Enter 3 for Advanced");
                int result;
                if (int.TryParse(Console.ReadLine(), out result))
                 {
                    if (result == 1)
                    {
                        Console.WriteLine("Welcome to Beginner, " + name);
                        success = true;
                    }
                    else if (result == 2)
                    {
                        Console.WriteLine("Welcome to Intermediate, " + name);
                        success = true;
                    }
                    else if (result == 3)
                    {
                        Console.WriteLine("Welcome to Advanced, " + name);
                        success = true;
                    }
                    else
                    {
                        Console.WriteLine("Incorrect Response");
                    }
                 }
    } until (success);

    I recommend that you always write all your braces { and } in the if's and else's, even when they are unnecessary because there is only a single statement. It will save you from making many errors.

    • Marked as answer by CoolMuffin Monday, October 19, 2020 8:08 AM
    Sunday, October 18, 2020 4:23 PM
    Moderator

All replies

  • There are different ways in which you can write the loop, but one of them is to define a boolean variable called "success" and loop until success is true. Of course, you initialize it to false and you set it to true in the cases where a correct number is entered:

    Console.Write("What is your name: ");
                string name = Console.ReadLine();
    
    bool success = false;
    do
    {
                Console.WriteLine("Welcome " + name + " to the Spelling Game");
                Console.WriteLine("Enter 1 for Beginner");
                Console.WriteLine("Enter 2 for Intermediate");
                Console.WriteLine("Enter 3 for Advanced");
                int result;
                if (int.TryParse(Console.ReadLine(), out result))
                 {
                    if (result == 1)
                    {
                        Console.WriteLine("Welcome to Beginner, " + name);
                        success = true;
                    }
                    else if (result == 2)
                    {
                        Console.WriteLine("Welcome to Intermediate, " + name);
                        success = true;
                    }
                    else if (result == 3)
                    {
                        Console.WriteLine("Welcome to Advanced, " + name);
                        success = true;
                    }
                    else
                    {
                        Console.WriteLine("Incorrect Response");
                    }
                 }
    } until (success);

    I recommend that you always write all your braces { and } in the if's and else's, even when they are unnecessary because there is only a single statement. It will save you from making many errors.

    • Marked as answer by CoolMuffin Monday, October 19, 2020 8:08 AM
    Sunday, October 18, 2020 4:23 PM
    Moderator

  • Hi CoolMuffin,

    Call me picky, but I'd prefer to use a switch/case for this code. Borrowing from Alberto's example and changing it a bit, I think this is a little cleaner:

    Console.Write("What is your name: ");
    string name = Console.ReadLine();
    Console.WriteLine("Welcome " + name + " to the Spelling Game");
    Console.WriteLine("Enter 1 for Beginner");
    Console.WriteLine("Enter 2 for Intermediate");
    Console.WriteLine("Enter 3 for Advanced");
    
    bool success = true;
    do
    {
        int result;
        if (int.TryParse(Console.ReadLine(), out result))
         {
            switch (result)
            {
                case 1:
                    Console.WriteLine("Welcome to Beginner, " + name);
                    break;
                case 2:
                    Console.WriteLine("Welcome to Intermediate, " + name);
                    break;
                case 3:
                    Console.WriteLine("Welcome to Advanced, " + name);
                    break;
                default:
                    Console.WriteLine("Incorrect Response");
                    success = false;
                    break;
            }
         }
    } until (success);
    
    
    

    As I said, I'm being picky and, as Alberto said, there are many ways to write this.

    I like setting the success boolean to true at the start and only setting it to false when necessary.

    Also note that I took out the excess Console.WriteLines out of the loop. I don't think they need to be repeated in the loop when the user enters an incorrect response, but feel free to put them in the loop if you prefer.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, October 18, 2020 5:58 PM
    Moderator
  • I thought about setting the variable to true by default and only changing to false if all the conditions failed... but there is a problem in doing it this way. It the TryParse fails because the user entered something that is not recognizable as a number, then your loop will exit with success=true and not ask for a new input. You can fix this by setting success to false when the TryParse fails, but then you need another "else" condition and it becomes a bit more complicated. I opted for keeping it as close as possible to the original code for the benefit of the original poster, but of course it can be rewritten in many ways as we have both mentioned before.
    Sunday, October 18, 2020 6:29 PM
    Moderator
  • One more alternative is:

    int result;
    while( !int.TryParse( Console.ReadLine( ), out result ) || result < 1 || result > 3 )
    {
       Console.WriteLine( "Incorrect Response" );
    }
    

    After this loop, result is valid, you can display the welcome message and continue the program.

    Sunday, October 18, 2020 6:46 PM
  • I thought about setting the variable to true by default and only changing to false if all the conditions failed... but there is a problem in doing it this way. It the TryParse fails because the user entered something that is not recognizable as a number, then your loop will exit with success=true and not ask for a new input. 

    Yep, good point Alberto! I missed that somehow ... didn't think it through enough I guess.  =0(   

    Thanks!  =0)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, October 18, 2020 8:27 PM
    Moderator
  • Ooooh, I *like* that Viorel !! Very concise!  =0)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, October 18, 2020 8:31 PM
    Moderator
  • Thanks
    Monday, October 19, 2020 8:17 AM
  • Thanks,

    for contributing

    Monday, October 19, 2020 8:17 AM
  • Thanks,

    for contributing

    Monday, October 19, 2020 8:22 AM