C
C#11h ago
GreenMile

Menu Validation 'char input'

I just asking for some general directions to design a solid input validation for a menu. It 'works' but I feel like I am using the wrong tools. I want it to only use a key press, that is fine. The overall come I wrote doesn't feel solid.
static void Main()
{
List<string> fruitList = new List<string>();
bool IsMenuActive = true;

while (IsMenuActive)
{
//Console.WriteLine("MainMenu\\");
Console.WriteLine("[1] - Menu1");
Console.WriteLine("[2] - Menu2");
Console.WriteLine("[3] - Menu3");
Console.WriteLine("[4] - Menu4");
Console.WriteLine();
ValidKeyPress();
}




static char ValidKeyPress()
{

Console.Write("Choose menu: ");
char userInput = Console.ReadKey().KeyChar;
while (!char.IsDigit(userInput))
{
Console.WriteLine();
Console.Write("Wrong keypress, try again: ");
userInput = Console.ReadKey(true).KeyChar;
Console.Write(userInput);
}

Console.WriteLine();
return userInput;



}
static void Main()
{
List<string> fruitList = new List<string>();
bool IsMenuActive = true;

while (IsMenuActive)
{
//Console.WriteLine("MainMenu\\");
Console.WriteLine("[1] - Menu1");
Console.WriteLine("[2] - Menu2");
Console.WriteLine("[3] - Menu3");
Console.WriteLine("[4] - Menu4");
Console.WriteLine();
ValidKeyPress();
}




static char ValidKeyPress()
{

Console.Write("Choose menu: ");
char userInput = Console.ReadKey().KeyChar;
while (!char.IsDigit(userInput))
{
Console.WriteLine();
Console.Write("Wrong keypress, try again: ");
userInput = Console.ReadKey(true).KeyChar;
Console.Write(userInput);
}

Console.WriteLine();
return userInput;



}
5 Replies
Salman
Salman11h ago
First of all $code
MODiX
MODiX11h ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Muhammad Hammad
Muhammad Hammad11h ago
just do if char=='1' or use switch to check cases from 1 to 4 and any other input will be invalid
if(userInput==1 |.| userInput==2 |.|userInput==3|.| userInput==4)
{
//input is valid do the work here
}
if(userInput==1 |.| userInput==2 |.|userInput==3|.| userInput==4)
{
//input is valid do the work here
}
leowest
leowest10h ago
you can resume it a bit further with
static char ValidKeyPress()
{
char userInput = '\0';
while (!char.IsDigit(userInput))
{
Console.WriteLine();
Console.Write("Provide a valid option: ");
userInput = Console.ReadKey().KeyChar;
}
return userInput;
}
static char ValidKeyPress()
{
char userInput = '\0';
while (!char.IsDigit(userInput))
{
Console.WriteLine();
Console.Write("Provide a valid option: ");
userInput = Console.ReadKey().KeyChar;
}
return userInput;
}
but in the end you would be using that result against a switch or some conditions to run more code @GreenMile this might be of your interest https://gist.github.com/ZacharyPatten/798ed612d692a560bdd529367b6a7dbd it shows various ways and scenarios on how to take console input and how simple it can be
Fanta
Fanta9h ago
@GreenMile What Leo said is good advice. Also, unsure if you need it but, you can provide ReadKey() with a boolean true to make it so that the pressed key doesn't appear in the terminal.
char letter = Console.ReadKey(true).KeyChar;
Console.WriteLine(letter); // holds the character you pressed
char letter = Console.ReadKey(true).KeyChar;
Console.WriteLine(letter); // holds the character you pressed
Want results from more Discord servers?
Add your server