0

Here is de ad of the excercise:

ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.

If the function is passed a valid PIN string, return true, else return false.

eg:

ValidatePin("1234") => true

ValidatePin("12345") => false

ValidatePin("a234") => false

And here is the code with the error:

using System;
using System.Text.RegularExpressions;

public class Kata
{
    public static bool ValidatePin(string pin)
    {
        int pinn; //int called pinn declared
        int cont=0; // the same that the count
        int i;  //and the variable i for identify the for
        for(i=0;i<9999;i++)
        {
            cont +=1;
        }
        Console.WriteLine("Please enter the PIN:"); //tell the user to type the PIN number
        Console.ReadLine(pinn); //read the num pinn
        if(pinn>cont) //if
        {
            Console.WriteLine("Wrong output for",pinn);
        }

        return true || false;
}

Error:

Time: 1889ms Exit Code: 1 Test Results: Log src/Solution.cs(16,13): error CS1501: No overload for method 'ReadLine' takes 1 arguments src/Solution.cs(16,22): error CS0165: Use of unassigned local variable 'pinn'

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Cpp
  • 29
  • 2
  • 7
  • `Console.ReadLine()` doesn't need any argument, please read this documentation https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=netcore-3.1 – zer09 May 13 '20 at 06:11
  • Does this answer your question? [Fastest way to check if string contains only digits](https://stackoverflow.com/questions/7461080/fastest-way-to-check-if-string-contains-only-digits) – Pavel Anikhouski May 13 '20 at 06:39
  • `Console.Readline` is a function that returns a value, check [its documentation](https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=netframework-4.7.2#System_Console_ReadLine). You have other errors also, like the wrong `Console.WriteLine`, that weird `for` loop and that `return true || false`. – Cleptus May 13 '20 at 07:00
  • `bool valid = pin != null && pin.All(c => c >= '0' && c <= '9') && (pin.Length == 4 || pin.Length == 6);` – Dmitry Bychenko May 13 '20 at 07:02

3 Answers3

0

As error says, In C# there is no overloaded Console.ReadLine() method which accept one argument, ReadLine method in Console class reads input from user and stores it in string variable.

As your pinn variable is of type int, you need to convert input from Console.ReadLine() to int like,

pinn = Convert.ToInt32(Console.ReadLine());

Now you assigned some value to pinn variable, so you will not face the second error.


There is lot of improvement that you can do in your program, your goal is to check that length of pin is 4 or 6. Return true if length is 4 or 6 otherwise false,

public class Kata
{
    public static bool ValidatePin(string pin)
    {

       //below condition will return true if length is 4 or 6, otherwise false
       var condition = !string.IsNullOrEmpty(pin)    //Null check for pin
                          && (pin.Length == 4 || pin.Length == 6)  //Length should be 4 or 6
                          && pin.All(char.IsDigit); // Check for all digits

       return condition;
    }
}

.NET FIDDLE

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • Counter example: `pin = "-123"` expected `false`, actual `true`; another counter example: `pin = " 123"` (please, note leading space) – Dmitry Bychenko May 13 '20 at 07:08
  • @DmitryBychenko thanks for these corner cases. I fixed it using `char.IsDigit` property – Prasad Telkikar May 13 '20 at 07:17
  • An academic counter example (`pin = "۱۲3۴"` expected `false`, actual `true`) with *Persian* digits still exits, but I doubt if it or alike will appear in real life – Dmitry Bychenko May 13 '20 at 07:22
0

In your exercise you are asked to only allow 4 or 6 digits. There are many ways to check this. The easiest is with a regular expression, in your program you are already using its reference.

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static bool ValidatePin(string pin)
    {
        Regex rgx = new Regex(@"^\d{4}(?:\d{2})?$");
        return rgx.IsMatch(pin);
    }

    public static void Main()
    {
        Console.WriteLine("Insert pin:"); 
        string pin = Console.ReadLine();

        Console.WriteLine("The restult is {0}", ValidatePin(pin));
    }
}
TWP
  • 250
  • 1
  • 5
  • 13
  • More accurate implementation is `new Regex(@"^\d{4}(?:\d{2})?$", RegexOptions.ECMAScript);` otherwise `pin = "۱۲۳۴"` (note *Persian* digits) will match – Dmitry Bychenko May 13 '20 at 07:13
0

Why not a simple direct check? pin is valid if and only if

  1. pin is not null
  2. pin.Length is either 4 or 6
  3. All characters within pin are digits within '0'..'9' range

Code:

using System.Linq;

...

public static bool ValidatePin(string pin) =>
   pin != null && 
  (pin.Length == 4 || pin.Length == 6) &&         
   pin.All(c => c >= '0' && c <= '9'); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215