Exercise: Secure ATM PIN Validation
Problem statement
An ATM system requires a user to enter a 4-digit numeric PIN to gain access to their account. Security protocols dictate that the user should only have a maximum of three attempts to enter the correct PIN. If they fail all three times, the system should lock the account for security reasons.
Task requirements
Store a correct 4-digit PIN as a constant integer (e.g.,
1234).Track the number of attempts the user has made, starting from one.
Prompt the user for their PIN and safely parse it to ensure it is a valid number.
Compare the user's input to the correct PIN.
Exit the loop immediately if the PIN is correct and display a success message.
If the PIN is incorrect, inform the user how many attempts they have left.
If all three attempts are exhausted without a correct entry, display an account lockout message.
Constraints
Use a
whileordo-whileloop to manage the repeated prompts.Use
int.TryParseto ensure the program handles non-numeric characters gracefully.Use the
breakoperator to terminate the loop early upon successful login.Use string interpolation for all console messages.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use a variable to track the current attempt number and increment it inside the loop.
The loop condition should check if the number of attempts is still less than the maximum allowed.
Remember that
int.TryParserequires anoutvariable to store the result of the conversion.
Exercise: Secure ATM PIN Validation
Problem statement
An ATM system requires a user to enter a 4-digit numeric PIN to gain access to their account. Security protocols dictate that the user should only have a maximum of three attempts to enter the correct PIN. If they fail all three times, the system should lock the account for security reasons.
Task requirements
Store a correct 4-digit PIN as a constant integer (e.g.,
1234).Track the number of attempts the user has made, starting from one.
Prompt the user for their PIN and safely parse it to ensure it is a valid number.
Compare the user's input to the correct PIN.
Exit the loop immediately if the PIN is correct and display a success message.
If the PIN is incorrect, inform the user how many attempts they have left.
If all three attempts are exhausted without a correct entry, display an account lockout message.
Constraints
Use a
whileordo-whileloop to manage the repeated prompts.Use
int.TryParseto ensure the program handles non-numeric characters gracefully.Use the
breakoperator to terminate the loop early upon successful login.Use string interpolation for all console messages.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use a variable to track the current attempt number and increment it inside the loop.
The loop condition should check if the number of attempts is still less than the maximum allowed.
Remember that
int.TryParserequires anoutvariable to store the result of the conversion.
// ATM Security System
const int correctPin = 1234;
int attempts = 0;
const int maxAttempts = 3;
Console.WriteLine("=== ATM PIN Validation ===");
// TODO: Implement a loop that allows up to 3 attempts
// 1. Prompt the user for a PIN
// 2. Use int.TryParse for safety
// 3. Compare the PIN and handle the success/fail cases
// 4. Use 'break' if the user gets it right