-2
  static void MainWindow()
    {
        
        Console.Clear();
        Console.WriteLine("Menu");
        Console.WriteLine("");
        Console.WriteLine("1. Information");
        Console.WriteLine("2. Contact");
        Console.WriteLine("3. Extra");
        Console.WriteLine("q. Exit");
        string myOptions;
        myOptions = Console.ReadLine();

        switch (myOptions)
        {
            case "1":
                Information();
                break;
            case "2":
                Contact();
                break;
            case "3":
                Extra();
                break;
            case "q":
                Exit();
                break;

            default:
                Console.Clear();
                Console.WriteLine("Input is wrong");
                Console.ReadKey();
                MainWindow();
                break;
                


        }

        Console.ReadLine();
    }

I make a console menu and I need a counter that will count wrong entries. if it hits 5 times, the user is thrown back to the main menu.

I was trying to do this with while statement but it didnt work for me.

Jim G.
  • 15,141
  • 22
  • 103
  • 166
d0berr
  • 3
  • 1

1 Answers1

0
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace ConsoleApp3
{
    internal class Program
    {
        private static readonly IDictionary<char, Action> InputDictionary = new Dictionary<char, Action>
        {
            { '1', Information },
            { '2', Contact },
            { '3', Extra },
            { 'q', Exit },
        };

        static void Main()
        {
            MainWindow();
        }

        private static void MainWindow()
        {
            var inputWasProcessed = false;
            while (inputWasProcessed == false)
            {
                DisplayMainMenu();
                var wrongEntries = 0;
                while (wrongEntries < 5)
                {
                    if (0 < wrongEntries)
                    {
                        Console.WriteLine($"Wrong Entries: {wrongEntries}");
                    }

                    inputWasProcessed = ProcessUserInput(Console.ReadKey().KeyChar);
                    if (inputWasProcessed == false)
                    {
                        wrongEntries++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            Console.ReadLine();
        }

        private static void DisplayMainMenu()
        {
            Console.Clear();
            Console.WriteLine("Menu");
            Console.WriteLine();
            Console.WriteLine("1. Information");
            Console.WriteLine("2. Contact");
            Console.WriteLine("3. Extra");
            Console.WriteLine("q. Exit");
        }

        private static bool ProcessUserInput(char selectedOption)
        {
            if (InputDictionary.TryGetValue(selectedOption, out var action))
            {
                Console.WriteLine();
                Console.WriteLine();
                action.Invoke();
                return true;
            }

            Console.Clear();
            Console.WriteLine("Input is wrong.");
            Console.WriteLine("Try again.");
            return false;
        }


        private static void Information()
        {
            Console.WriteLine($"{GetCurrentMethod()} implementation.");
        }

        private static void Contact()
        {
            Console.WriteLine($"{GetCurrentMethod()} implementation.");
        }

        private static void Extra()
        {
            Console.WriteLine($"{GetCurrentMethod()} implementation.");
        }

        private static void Exit()
        {
            Console.WriteLine($"{GetCurrentMethod()} implementation.");
        }

        #region For Demonstration Purposes Only
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static string GetCurrentMethod() // https://stackoverflow.com/a/2652481/109941
        {
            var st = new StackTrace();
            var sf = st.GetFrame(1);

            return sf.GetMethod().Name;
        }
        #endregion
    }
}
Jim G.
  • 15,141
  • 22
  • 103
  • 166