-1

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.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
AlecTech
  • 63
  • 8

2 Answers2

0

The main problem is that your code does not follow the natural course of the action. Here is my suggestion (making use of the System.Text.RegularExpressions namespace):

static Regex PinRegex = new Regex(@"\d{4}");

static bool isPin(string s) => PinRegex.IsMatch(s);

static bool isExit(string s) => s == "q";

static void PopulateArray(string[] theArray)
{
    for (int i = 0; i < theArray.Length; i++)
    {
        Console.Write($"Please enter 4-Digit PIN or q to exit #{i + 1}: ");
        string userAnswer = Console.ReadLine();
        if (isExit(userAnswer))
            exit();  // To be implemented
        else if (isPin(userAnswer))
            theArray[i] = userAnswer;
        else
            unrecognizedAnswer(); // To be implemented
    }
}
KloppyToppy
  • 244
  • 2
  • 5
-1

Easy, you want a string, you can do 2 things, I will give you some options:

First option, the parameter is always a string:

static string GetValidInt(string prompt, int min, int max)
{
    bool valid = false;
    string myInt = "-1";
    //string myInt; //trying to convert a int to string
do
{
Console.Write(prompt);
try
{
//myInt = Console.ReadLine();
myInt = Console.ReadLine(); 
  if (int.Parse(myInt)< min || int.TryParse(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;
}

Second option will be to realise why you need myInt to be a string, I believe that you are trying to pass the string that you are reading, in that case, I would code this differently, I can show you the code, but I think that will be out of the scope of the question. If that's what you are after, have a look at https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=netcore-3.1

Iria
  • 433
  • 1
  • 8
  • 20
  • 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 – AlecTech Aug 15 '20 at 18:02
  • 1.- to check that the user enters a number you can still store it in a string, and then check that it is a number and parse it (https://stackoverflow.com/questions/894263/identify-if-a-string-is-a-number). 2.- that means, that the array is an array of strings or ints? what would you like, you can do any! – Iria Aug 15 '20 at 22:29