Y/N or y/n in loop

Dwayne Radar picture Dwayne Radar · Jan 15, 2013 · Viewed 8.3k times · Source

I have trouble implementing the Y/N or y/n in the loop. I've designed it in a way that a user can use both the capital and small letters of the Y and N for their answer in a loop. by the way here's my code but can't seem to make it work:

do
        {
            Console.WriteLine("\nSelect additional topping/s\n");

            Console.WriteLine("1 - Extra meat: 200");
            Console.WriteLine("2 - Extra cheese: 100");
            Console.WriteLine("3 - Extra veggies: 80\n");

            int selectedTopping = Convert.ToInt32(Console.ReadLine());

            switch (selectedTopping)
            {
                case 1:
                    pizza = new MeatToppings(pizza);
                    break;

                case 2:
                    pizza = new CheeseToppings(pizza);
                    break;

                case 3:
                    pizza = new VeggieToppings(pizza);
                    break;

                default:
                    break;
            }

            Console.WriteLine("\nAdd more toppings? Y/N");
            

        }

        while ((Console.ReadLine() == "Y") || (Console.ReadLine() == "y"));

Answer

Austin Henley picture Austin Henley · Jan 15, 2013
while ((Console.ReadLine() == "Y") || (Console.ReadLine() == "y"));

This is going to read 2 different lines since you're calling ReadLine() twice. You need to call it once and save the value.