Hello
I`m new with C# and I want to make a simple C# pin code program where users could only input 3 times or put in 3 times attempts of pincode and the the program will exit using Application.Exit();
but it seems that the loop is making the MessageBox quite annoying continuously and dont allow users to press or key in button for the next 2 attempts.
Help me pls anyone?
Edited with this but the error of “use of unnasigned local variable of ‘pin'” T_T :
private int _failedAttempts = 0;
private void btnEtr_Click(object sender, System.EventArgs e)
{
int pin;
if (pin != 21)
{
if (pin !=21)
{
_failedAttempts++;
MessageBox.Show ("Fail. " + (3 - _failedAttempts) + " attempts more.", "EPIC FAIL", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if(_failedAttempts == 3)
{
Application.Exit();
}
}
}
else
{
MessageBox.Show ("Welcome. Your pin is CORRECT", "CONGRATULATIONS",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
Right now you’re basically telling the user that they’ve failed, decreasing the counter, and then telling them they’ve failed again.
You are not giving them the chance to re-enter the pin number. You need to keep track of the attempts outside of the button click event. Declare a class level variable that will keep track of the attempts.
e.g.
And change your button click to something like:
This lets the user enter the pincode, if it fails, it will display the error message and increase the number of attempts. You should let them enter the pin number again, and let them hit the enter button again. When it hits 3 attempts it will exit.