C# Console Application
-
bigjoe11a wrote:
Some of it works and some of it doesn't
Are we supposed to guess which parts work and which don't? Every time you use a
goto
a kitten gets punched in the face. Why do you hate kittens so much?:Badger:
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; } -----------------------------------------
-
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; } -----------------------------------------
Seriously,
goto
s 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:
-
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; } -----------------------------------------
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
-
Seriously,
goto
s 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:
-
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
>>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.
-
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
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:
-
>>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.
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");
-
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");
-
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
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
-
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 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 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