This method asks for a string between min and max characters
and then asks for confirmation on entering a valid string
please let me know if it can be done more efficiently etc etc
and then asks for confirmation on entering a valid string
please let me know if it can be done more efficiently etc etc
C#:
public static string GetLimitedStringWithConfirmation(string prompt, int min, int max)
{
do
{
Console.WriteLine(prompt);
var userString = Console.ReadLine();
if (userString.Length >= min && userString.Length <= max)
{
Console.Write($"You entered {userString} Do you accept [Y/N]: ");
char ch = Console.ReadKey(true).KeyChar;
ch = char.ToUpper(ch);
switch (ch)
{
case 'Y':
return userString;
case 'N':
Console.WriteLine("\nThe entry was rejected...");
break;
}
}
} while (true);
}
Last edited by a moderator: