'Error in this program of c# validate pin code?
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'
Solution 1:[1]
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));
}
}
Solution 2:[2]
Why not a simple direct check? pin is valid if and only if
pinis notnullpin.Lengthis either4or6Allcharacters withinpinare 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');
Solution 3:[3]
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;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TWP |
| Solution 2 | Dmitry Bychenko |
| Solution 3 |
