GreenMile
GreenMile
Explore posts from servers
CC#
Created by GreenMile on 11/2/2024 in #help
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;



}
12 replies
CC#
Created by GreenMile on 8/30/2024 in #help
Beginner questions. prevent unhalted exception
No description
39 replies
CC#
Created by GreenMile on 8/29/2024 in #help
Run Powershell inside VS
No description
11 replies