Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Console Application

C# Console Application

Scheduled Pinned Locked Moved C#
csharpgame-devhelptutorial
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B bigjoe11a

    Ok, why not. LOL. I mean it doesn't work at all. When I press 1. it just returns to the menu. and so on. Or the default just loads. DO you know of a sample source code that will show me how to do this the right way. -------------------------------------------------------------------- MainMenu: terminal.ClearScreen(); terminal.WriteLine(); terminal.WriteLine("Welcome :" + name); terminal.WriteLine("1) Game Menu"); terminal.WriteLine("2) List Users"); terminal.WriteLine("3) Help"); terminal.WriteLine("9) Quit"); terminal.Write("Choice : (1,2,3 0r 9)"); int caseSwitch = terminal.ReadDigit(); switch (caseSwitch) { case 1: Functions NewGame = new Functions(); NewGame.GameMenu(); goto MainMenu; case 2: terminal.WriteLine("Case 2"); goto MainMenu; case 3: Functions NewHelp = new Functions(); NewHelp.Help(); goto MainMenu; case 9: break; default: terminal.WriteLine("Invalid Key"); terminal.PromptEnter("Press ENTER to Continue.."); goto MainMenu; } -----------------------------------------

    J Offline
    J Offline
    Jimmanuel
    wrote on last edited by
    #4

    Seriously, gotos are the biggest indicator in the world of poorly structured code. Code flow should be handle by conditionals and loops and I'm guessing that's the main cause of your problems. My beginners attempt would start like this: Semi-Psuedo Code:

    bool done = false;
    while (!done)
    {
    int userInput = 0;
    do
    {
    PrintMainMenu();
    userInput = GetNextInput();
    } while (!InputIsValid(userInput));

    switch (userInput)
    {
        case 0:
            PrintHelpMenu();
            break;
        case 1:
            RunGamesMenu();
            break;
        case 2:
            Console.WriteLine("Exiting");
            done = true;
            break;
        // and so on and so on   
    }
    

    }

    P.S. put pre tags around code snippets to preserve formatting

    :Badger:

    B 1 Reply Last reply
    0
    • B bigjoe11a

      Ok, why not. LOL. I mean it doesn't work at all. When I press 1. it just returns to the menu. and so on. Or the default just loads. DO you know of a sample source code that will show me how to do this the right way. -------------------------------------------------------------------- MainMenu: terminal.ClearScreen(); terminal.WriteLine(); terminal.WriteLine("Welcome :" + name); terminal.WriteLine("1) Game Menu"); terminal.WriteLine("2) List Users"); terminal.WriteLine("3) Help"); terminal.WriteLine("9) Quit"); terminal.Write("Choice : (1,2,3 0r 9)"); int caseSwitch = terminal.ReadDigit(); switch (caseSwitch) { case 1: Functions NewGame = new Functions(); NewGame.GameMenu(); goto MainMenu; case 2: terminal.WriteLine("Case 2"); goto MainMenu; case 3: Functions NewHelp = new Functions(); NewHelp.Help(); goto MainMenu; case 9: break; default: terminal.WriteLine("Invalid Key"); terminal.PromptEnter("Press ENTER to Continue.."); goto MainMenu; } -----------------------------------------

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #5

      Rule 1: Use the "code block" button below to preserve formatting. It makes things easier to read. Rule 2: unless you are a seriously experienced programmer and there is a very good reason, do not use goto. It will earn you nothing but grief and abuse here. Rule 3: Learn about loops. Learn about data types. Learn at least the basics of C#. Rule 4: Learn there is a difference between "Console" and "Terminal" (one exists, the other doesn't) Having said that, I'm in a good mood:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace Demo
      {
      class Program
      {
      static void Main(string[] args)
      {
      string name;
      Console.Write("Enter your name: ");
      name = Console.ReadLine();

              Console.WriteLine("\\nWelcome : " + name);
              Console.WriteLine("1) Game Menu");
              Console.WriteLine("2) List Users");
              Console.WriteLine("3) Help");
              Console.WriteLine("9) Quit");
              Console.Write("Choice : (1,2,3 or 9) ");
              int i = 0;
              do
                  {
                  string s = Console.ReadLine();
                  if (s == "")
                      {
                      continue;
                      }
                  i = s\[0\];
                  switch (i)
                      {
                      case '1':
                          Console.WriteLine("Game selected");
                          // Handle game.
                          break;
                      case '2':
                          Console.WriteLine("List selected");
                          // List users
                          break;
                      case '3':
                          Console.WriteLine("Help selected");
                          // Help
                          break;
                      case '9':
                          Console.WriteLine("Quit selected");
                          break;
                      default:
                          Console.WriteLine("Invalid Key");
                          break;
                      }
                  }
              while (i != '9');
              }
          }
      }
      

      This is not perfect (I threw it together from your code), but it has the advantage of working... Now, for your homework, please post a complete explaination of how it works.

      No trees were harmed in the sending of this message; however, a significant number of electrons were slig

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • J Jimmanuel

        Seriously, gotos are the biggest indicator in the world of poorly structured code. Code flow should be handle by conditionals and loops and I'm guessing that's the main cause of your problems. My beginners attempt would start like this: Semi-Psuedo Code:

        bool done = false;
        while (!done)
        {
        int userInput = 0;
        do
        {
        PrintMainMenu();
        userInput = GetNextInput();
        } while (!InputIsValid(userInput));

        switch (userInput)
        {
            case 0:
                PrintHelpMenu();
                break;
            case 1:
                RunGamesMenu();
                break;
            case 2:
                Console.WriteLine("Exiting");
                done = true;
                break;
            // and so on and so on   
        }
        

        }

        P.S. put pre tags around code snippets to preserve formatting

        :Badger:

        B Offline
        B Offline
        bigjoe11a
        wrote on last edited by
        #6

        Thanks, Can you give me an idea about what the PrintMainMenu would look like and a sample of the code used to make it. Or make be I'm getting to old to program. Thanks

        J 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Rule 1: Use the "code block" button below to preserve formatting. It makes things easier to read. Rule 2: unless you are a seriously experienced programmer and there is a very good reason, do not use goto. It will earn you nothing but grief and abuse here. Rule 3: Learn about loops. Learn about data types. Learn at least the basics of C#. Rule 4: Learn there is a difference between "Console" and "Terminal" (one exists, the other doesn't) Having said that, I'm in a good mood:

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace Demo
          {
          class Program
          {
          static void Main(string[] args)
          {
          string name;
          Console.Write("Enter your name: ");
          name = Console.ReadLine();

                  Console.WriteLine("\\nWelcome : " + name);
                  Console.WriteLine("1) Game Menu");
                  Console.WriteLine("2) List Users");
                  Console.WriteLine("3) Help");
                  Console.WriteLine("9) Quit");
                  Console.Write("Choice : (1,2,3 or 9) ");
                  int i = 0;
                  do
                      {
                      string s = Console.ReadLine();
                      if (s == "")
                          {
                          continue;
                          }
                      i = s\[0\];
                      switch (i)
                          {
                          case '1':
                              Console.WriteLine("Game selected");
                              // Handle game.
                              break;
                          case '2':
                              Console.WriteLine("List selected");
                              // List users
                              break;
                          case '3':
                              Console.WriteLine("Help selected");
                              // Help
                              break;
                          case '9':
                              Console.WriteLine("Quit selected");
                              break;
                          default:
                              Console.WriteLine("Invalid Key");
                              break;
                          }
                      }
                  while (i != '9');
                  }
              }
          }
          

          This is not perfect (I threw it together from your code), but it has the advantage of working... Now, for your homework, please post a complete explaination of how it works.

          No trees were harmed in the sending of this message; however, a significant number of electrons were slig

          B Offline
          B Offline
          bigjoe11a
          wrote on last edited by
          #7

          >>Now, for your homework, please post a complete explaination of how it works Home work. Grin. I haven't done any home work in over 30+ years. and to explaination of how it works. ? I have no idea. It mite take some time before I can even under stand what you did. Any chance I can get you to make a simple one.

          OriginalGriffO 1 Reply Last reply
          0
          • B bigjoe11a

            Thanks, Can you give me an idea about what the PrintMainMenu would look like and a sample of the code used to make it. Or make be I'm getting to old to program. Thanks

            J Offline
            J Offline
            Jimmanuel
            wrote on last edited by
            #8

            it would contain something like this (from your original post):

            terminal.ClearScreen();
            terminal.WriteLine();
            terminal.WriteLine("Welcome :" + name);
            terminal.WriteLine("1) Game Menu");
            terminal.WriteLine("2) N/A");
            terminal.WriteLine("3) Help");
            terminal.WriteLine("9) Quit");

            I just stuck a function there to save space in the post :)

            :Badger:

            1 Reply Last reply
            0
            • B bigjoe11a

              >>Now, for your homework, please post a complete explaination of how it works Home work. Grin. I haven't done any home work in over 30+ years. and to explaination of how it works. ? I have no idea. It mite take some time before I can even under stand what you did. Any chance I can get you to make a simple one.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #9

              Oooo! That is a simple one! I can't teach you C# - I haven't got the time - but I'll do what I can. Have you got any tools yet (C# compiler, etc)? If not, go to microsoft.com and search for "C# 2008 Express download" - this will give you a free copy of the C# IDE, with all the tools you need to do nearly everything with C#. Do get a book on C# - there are some good ones out there, but I am not sure what you would need for starters. There is a free one available here C# Yellow book[^] which is used by the University of Hull for 1st year CS students and should cover the language basics. Now, what have we got:

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;

              namespace Demo
              {
              class Program
              {
              static void Main(string[] args)
              {

              ...

                      }
                  }
              }
              

              This is all overhead - it is automatically generated for you and provides a "framework" in which your program can run.

                      string name;
                      Console.Write("Enter your name: ");
                      name = Console.ReadLine();
              

              All this does is declare a variable called "name" that can hold a string of text, prompt the user for his name, and read his response.

                      Console.WriteLine("\\nWelcome :" + name);
                      Console.WriteLine("1) Game Menu");
                      Console.WriteLine("2) List Users");
                      Console.WriteLine("3) Help");
                      Console.WriteLine("9) Quit");
                      Console.Write("Choice : (1,2,3 or 9) ");
              

              Print out the users selection of choices.

                      int i = 0;
                      do
                          {
              

              ...

                          }
                      while (i != '9');
              

              This sets up a loop - the program will go around and around here until the variable "i" contains the value '9', then it will exit the loop.

                          string s = Console.ReadLine();
                          if (s == "")
                              {
                              continue;
                              }
              

              Read the users menu selection into the variable "s", check that he didn't just press the ENTER key (if he did, ignore it and got back around the loop).

                          i = s\[0\];
                          switch (i)
                              {
                              case '1':
                                  Console.WriteLine("Game selected");
              

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              B 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Oooo! That is a simple one! I can't teach you C# - I haven't got the time - but I'll do what I can. Have you got any tools yet (C# compiler, etc)? If not, go to microsoft.com and search for "C# 2008 Express download" - this will give you a free copy of the C# IDE, with all the tools you need to do nearly everything with C#. Do get a book on C# - there are some good ones out there, but I am not sure what you would need for starters. There is a free one available here C# Yellow book[^] which is used by the University of Hull for 1st year CS students and should cover the language basics. Now, what have we got:

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;

                namespace Demo
                {
                class Program
                {
                static void Main(string[] args)
                {

                ...

                        }
                    }
                }
                

                This is all overhead - it is automatically generated for you and provides a "framework" in which your program can run.

                        string name;
                        Console.Write("Enter your name: ");
                        name = Console.ReadLine();
                

                All this does is declare a variable called "name" that can hold a string of text, prompt the user for his name, and read his response.

                        Console.WriteLine("\\nWelcome :" + name);
                        Console.WriteLine("1) Game Menu");
                        Console.WriteLine("2) List Users");
                        Console.WriteLine("3) Help");
                        Console.WriteLine("9) Quit");
                        Console.Write("Choice : (1,2,3 or 9) ");
                

                Print out the users selection of choices.

                        int i = 0;
                        do
                            {
                

                ...

                            }
                        while (i != '9');
                

                This sets up a loop - the program will go around and around here until the variable "i" contains the value '9', then it will exit the loop.

                            string s = Console.ReadLine();
                            if (s == "")
                                {
                                continue;
                                }
                

                Read the users menu selection into the variable "s", check that he didn't just press the ENTER key (if he did, ignore it and got back around the loop).

                            i = s\[0\];
                            switch (i)
                                {
                                case '1':
                                    Console.WriteLine("Game selected");
                
                B Offline
                B Offline
                bigjoe11a
                wrote on last edited by
                #10

                Hi! OriginalGriff, Thank you very much. That was very easy for me to understand. It works. Thank You Can you tell me how to make the loop into a hot key. Where the user doesn't have to press enter. Just the number Thanks Joe

                OriginalGriffO 1 Reply Last reply
                0
                • B bigjoe11a

                  Hi! OriginalGriff, Thank you very much. That was very easy for me to understand. It works. Thank You Can you tell me how to make the loop into a hot key. Where the user doesn't have to press enter. Just the number Thanks Joe

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #11

                  Replace

                             string s = Console.ReadLine();
                             if (s == "")
                                 {
                                 continue;
                                 }
                             i = s\[0\];
                  

                  with

                             ConsoleKeyInfo cki;
                             cki = Console.ReadKey(true);
                             i = (int) cki.Key;
                  

                  (the "true" prevents the key being echoed to the display.)

                  No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  B 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Replace

                               string s = Console.ReadLine();
                               if (s == "")
                                   {
                                   continue;
                                   }
                               i = s\[0\];
                    

                    with

                               ConsoleKeyInfo cki;
                               cki = Console.ReadKey(true);
                               i = (int) cki.Key;
                    

                    (the "true" prevents the key being echoed to the display.)

                    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                    B Offline
                    B Offline
                    bigjoe11a
                    wrote on last edited by
                    #12

                    Thank you very much. That works

                    OriginalGriffO 1 Reply Last reply
                    0
                    • B bigjoe11a

                      Thank you very much. That works

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #13

                      I take it you spotted that one then... :laugh:

                      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      B 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        I take it you spotted that one then... :laugh:

                        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                        B Offline
                        B Offline
                        bigjoe11a
                        wrote on last edited by
                        #14

                        Yes I did, Thanks again

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups