3 Replies - 2432 Views - Last Post: 13 December 2010 - 04:49 AM Rate Topic: -----

#1 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

while loop and if statement problem

Posted 12 December 2010 - 04:55 PM

this is a form that i have created to ask the user for a password and if they get it wrong three times then the program will close.

Problem: i run the program enter a wrong password, message box is displayed "wrong password only two tries left." then when i press ok to the first message box the second one comes up automatically, then i press ok again and the third message box comes up automatically.

without ever letting the user press ok to the first message box then they can go back to entering a password press enter on the form then the next message box should come up if password wrong and so fourth.

I have tried not using the while statement, then i thought i would put
 break; 
into each condition but that goes out of the loop and stops incrementing it. then I thought if I changed the messagebox buttons on the form to retry and cancel or even ok and cancel it would of maybe worked. I feel it is probably a simple error thats why ive tryed hard doing different things. but now ive decided to ask for help.

This is my code:

            string pas = "password";
            txtpas.Focus();

int i = 0;
            while (this.txtpas.Text != pas && i<3)
            {          

    if (i == 0)
    {

       MessageBox.Show("You have entered the wrong password" +
                 " you only have two tries left!", "two tries ", MessageBoxButtons.RetryCancel);
               
    }

    else if (i == 1)
    {
        MessageBox.Show("You have entered the wrong password" +
                " you only have one try left!", "one try ", MessageBoxButtons.RetryCancel);
        
    }
    else if (i == 2)
    {                    
        MessageBox.Show("You have entered the wrong password" + " you have no tries left!" + 
            " This application will close", "Wrong password ", MessageBoxButtons.RetryCancel);
        Application.Exit();
        
    }
    i++;    
            } 




Many thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: while loop and if statement problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5566
  • View blog
  • Posts: 11,903
  • Joined: 02-June 10

Re: while loop and if statement problem

Posted 12 December 2010 - 05:18 PM

That is exactly correct behavior for the loop you have shown.

Since you never reset this.textpas.text the loop is going to continue as long as the conditions are met:
while (this.txtpas.Text != pas && i<3)
So pass one: Does his.textpas.Text != pas? yes. Is i<3? Yes. Keep looping
So pass two: Does his.textpas.Text != pas? yes. Is i<3? Yes. Keep looping
So pass three: Does his.textpas.Text != pas? yes. Is i<3? Yes. Keep looping

You have nothing in there stopping it.

Learn to debug.
Debugging tutorial
If you had put a breakpoint on line 1, and walked through your code using the F10 key you could have looked at the values of each variable. This allows you to see *why* something is happening *while* it is happening.

There is little code to go on. When is this loop executed? I would expect it to be when someone clicks a "LogIn" button for example. But we have no context for this code within the rest of your application.

Password handling tutorial

This post has been edited by tlhIn'toq: 12 December 2010 - 05:21 PM

Was This Post Helpful? 3
  • +
  • -

#3 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: while loop and if statement problem

Posted 13 December 2010 - 04:26 AM

Thanks.

Got this by putting breaks back in and incrementing before them as well as putting int i = o outside my scope event.

Thanks

This post has been edited by jemsel: 13 December 2010 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: while loop and if statement problem

Posted 13 December 2010 - 04:49 AM

i'd just take the loop out altogether. If you're using a button to submit the password, check the number of times the password has been submitted on the button click event. if it's less than three and password doesn't match you just don't do anything, if there's been 3 attempts and password still doesn't match then exit, else if the password matches then continue.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1