Search⌘ K
AI Features

Solution: Secure ATM PIN Validation

Understand how to create a secure ATM PIN validation system by using boolean flags, while loops, input parsing with int.TryParse, and conditional checks to authenticate user input safely and effectively.

We'll cover the following...
const int correctPin = 1234;
int attempts = 0;
const int maxAttempts = 3;
bool isAuthenticated = false;

Console.WriteLine("=== ATM PIN Validation ===");

while (attempts < maxAttempts)
{
    attempts++;
    Console.Write($"Attempt {attempts}/{maxAttempts}. Enter PIN: ");
    string input = Console.ReadLine();

    if (int.TryParse(input, out int enteredPin))
    {
        if (enteredPin == correctPin)
        {
            isAuthenticated = true;
            break; // Exit the loop immediately on success
        }
        else
        {
            Console.WriteLine("Incorrect PIN.");
        }
    }
    else
    {
        Console.WriteLine("Invalid input. Please enter numbers only.");
    }
}

if (isAuthenticated)
{
    Console.WriteLine("Access Granted. Welcome back!");
}
else
{
    Console.WriteLine("Too many failed attempts. Your account is locked.");
}
The complete solution of the ATM PIN validation logic using a while loop and break statement
...