0

I have coded what you see so far. basically need to make it that the user gets 3 tries. AFter the first fail, gives an error like " error: 2 more tries left", same again with the 2nd fail. Onm the third it brings a message up saying " Error: too many tries " then does this.Close() to close the program. I cannot seem to figure out where to put a maxattempts type thing because i tried it in the foreach and it makes the attempts 8. The rest of the code reads in from a text file, uses a class to split '|', and then stores in said variables.

public partial class Pin : Form
{
    private static List<Accounts> currentAccounts;
    public string pathToFile = "H:\\c#assessmentO\\accounts.txt";

    public Pin()
    {
        InitializeComponent();

        currentAccounts = File.ReadAllLines(pathToFile)
           .Skip(1)
           .Select(v => Accounts.FromPipe(v))
           .ToList();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string userAccNo = txtAccNo.Text;
        string userPinNo = txtPinNo.Text;
        string userForename = "";
        string userSurname = "";
        string userAddress = "";
        string userBalance = "";
        string userPurchasedToday = "";
        bool match = false;
        int lineNumber = 0;
        int numberOfLines = 0;
 
        if ((userAccNo == "999999") && (userPinNo == "4321"))
        {
            MessageBox.Show("SHUTDOWN ACTIVATED");
            this.Close();
        }
        else
        {
            foreach (Accounts Account in currentAccounts)
            {
                numberOfLines++;

                if ((Account.AccNu.ToString() == userAccNo) && (Account.PinNu.ToString() == userPinNo))
                {
                    match = true;
                    lineNumber = numberOfLines + 1;
                    userForename = Account.Forename.ToString();
                    userSurname = Account.Surname.ToString();
                    userAddress = Account.Address.ToString();
                    userBalance = Account.Balance.ToString();
                    userPurchasedToday = Account.PurchasesToday.ToString();
                }
            }
        }

        if (match == true)
        {
            MessageBox.Show("Access Granted: You can now access the System");

            Options frmOptions = new Options(userAccNo, userPinNo, pathToFile, lineNumber, userForename, userSurname, userAddress, userBalance, userPurchasedToday);
            this.Hide();
            frmOptions.ShowDialog();
        }
    }
}

Suppose to give only 3 tries for the password.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Feb 21 '23 at 15:47
  • Just add a variable `private int NumAttempts = 0;` Increment it after the `match == true` block. If it's three, exit the form. Your security isn't very secure, by the way. – LarsTech Feb 21 '23 at 15:54

2 Answers2

0

You might want to store private int userTries = 0; as a class field (as you do with currentAccounts), and then in the click event you can increment this number each time they try, and give them different messages depending on how many tries they've taken. You can also check the value at the beginning and close the form if they've already tried 3 times.

Showing only code relevant to the answer, it might look something like this:

public partial class Pin : Form
{
    private int userTries = 0;

    // Other class members omitted

    private void button1_Click(object sender, EventArgs e)
    {
        // Code to validate PIN omitted

        if (match == true)
        {
            userTries = 0;
            // Other code omitted
        }
        else
        {
            userTries++;
            
            if (userTries == 1) 
            {
                MessageBox.Show("Warning: You have 2 tries left.");
            }
            else if (userTries == 2) 
            {
                MessageBox.Show("Warning: You have 1 try left.");
            }
            else 
            {
                MessageBox.Show("Error: You have used all your tries and will be locked out.");
                this.Close();
            }
        }
    }
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

By implementing a variable that is initialized with the amount of tries you want as well as one that keeps track of the correct PIN is how you will want to adjust your code for it to max out at 3 attempts. The below example is one way you can account for this

public partial class Pin : Form
{
    private static List<Accounts> currentAccounts;
    public string pathToFile = "H:\c#assessmentO\accounts.txt";
    private int attemptsRemaining = 3;
    private bool pinEnteredCorrectly = false;
    
     public Pin()
     {
        InitializeComponent();
    
        currentAccounts = File.ReadAllLines(pathToFile)
           .Skip(1)
           .Select(v => Accounts.FromPipe(v))
           .ToList();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        if (attemptsRemaining == 0)
        {
            MessageBox.Show("Error: too many tries");
            this.Close();
            return;
        }
    
        string userAccNo = txtAccNo.Text;
        string userPinNo = txtPinNo.Text;
        string userForename = "";
        string userSurname = "";
        string userAddress = "";
        string userBalance = "";
        string userPurchasedToday = "";
        bool match = false;
        int lineNumber = 0;
        int numberOfLines = 0;
    
        foreach (Accounts Account in currentAccounts)
        {
            numberOfLines++;
    
            if ((Account.AccNu.ToString() == userAccNo) && (Account.PinNu.ToString() == userPinNo))
            {
                match = true;
                lineNumber = numberOfLines + 1;
                userForename = Account.Forename.ToString();
                userSurname = Account.Surname.ToString();
                userAddress = Account.Address.ToString();
                userBalance = Account.Balance.ToString();
                userPurchasedToday = Account.PurchasesToday.ToString();
            }
        }
    
        if (match == true)
        {
            MessageBox.Show("Access Granted: You can now access the System");
    
            Options frmOptions = new Options(userAccNo, userPinNo, pathToFile, lineNumber, userForename, userSurname, userAddress, userBalance, userPurchasedToday);
            this.Hide();
            frmOptions.ShowDialog();
            pinEnteredCorrectly = true;
        }
        else
        {
            attemptsRemaining--;
            if (attemptsRemaining > 0)
            {
                MessageBox.Show($"Error: {attemptsRemaining} more tries left");
            }
            else
            {
                MessageBox.Show("Error: too many tries");
                this.Close();
            }
        }
    }
}

Note that variable attemptsRemaining is initialized with 3 in the constructor and decremented every time the users enters the incorrect PIN. When it reaches 0 then it exits and throws an error.