Option 1 Add PINS , Option 4 Displays them (2 and 3 are not done), How can modify this code inside GetValidInt method to store myInt as a string?
static void PopulateArray(int[] theArray)
{
for (int i = 0; i < theArray.Length; i++)
{
theArray[i] = GetValidInt($"Please enter 4-Digit PIN or q to exit #{i + 1}: ", 0, 9999);
}
}
static int GetValidInt(string prompt, int min, int max)
{
bool valid = false;
int myInt = -1;
//string myInt; //trying to convert a int to string
do
{
Console.Write(prompt);
try
{
//myInt = Console.ReadLine();
myInt = int.Parse(Console.ReadLine());
if (myInt < min || myInt > max)
{
throw new Exception("Provided integer was outside of the bounds specified.");
}
valid = true;
}
catch (Exception ex)
{
Console.WriteLine($"Parse failed: {ex.Message}");
}
} while (!valid);
//enter code here
return myInt;
}
I want to check first that user enters a number between 0 and 9999 and that data can have leading "0" because those are PIN Codes( ex: "0001" or "0123"). Then I store them in the Array of [10] and retrieve them later per user request. Thats why i used int format first to chech for MIN and MAX and then I need to convert it to a string for storage so that I dont loose "zeros". I could limit my range from 999 to 10000 but then I wont be able to stores pins like "0001" or "0123" because it will store it as 1 and 123.