Jump to content

Console Application - Check input for keywords

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Joe N

Joe N

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi,

I'm writing a console application that will include a help menu. The program writes out "Type HELP for a list of available commands". What I want to figure out is how can I have the program constantly checking any input that enters the program and run it against a list of specified keywords, for example, HELP.

Some logic would be:

 public class helpCOMMANDS    {


        public void HELP()
        {






            if ((System.Console.ReadLine() == "HELP"))
            
            {


                Console.WriteLine("Output HELP commands here");


            }


        }


    }

My logic could be way off, but that is the general idea of what I'm looking to do.

Thanks.

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Make the Enum have whatever keywords you want and supply the methods for the Do...() routines. You can even put it in its own class!

using System;


namespace ConsoleApplication1 {

    class Program {

        static void Main(string[] args) {


            String word = Console.ReadLine();


            ProcessKeyword(word);


            Console.ReadLine();

        }


        static Boolean ProcessKeyword(String word) {

            Boolean result = true;

            Keywords key;


            if (Enum.TryParse<Keywords>(word, true, out key)) {

                switch (key) {

                    case Keywords.Cry: DoCryStuff(); break;

                    case Keywords.Eat: DoEatStuff(); break;

                    case Keywords.Exit: DoExitStuff(); break;

                    case Keywords.Help: DoHelpStuff(); break;

                    case Keywords.Jump: DoJumpStuff(); break;

                    case Keywords.Play: DoPlayStuff(); break;

                    case Keywords.Run: DoRunStuff(); break;

                    case Keywords.Whatever: DoWhatever(); break;

                    default: DoErrorStuff(); break;

                }

            } else {

                result = false;

                Console.WriteLine("Invalid Keyword!");

            }


            return result;

        }

    }


    enum Keywords {

        Help = 1,

        Run,

        Jump,

        Play,

        Cry,

        Eat,

        Whatever,

        Exit

    }

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users