5 Replies - 1230 Views - Last Post: 29 May 2012 - 07:24 AM Rate Topic: -----

#1 CDL2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-May 12

Looping and if statements

Posted 28 May 2012 - 10:15 AM

Hey guys, it's worth noting that I'm new to C# and have tried to lookup tutorials to help me fix this problem.

I'm trying to create a procedure whereby the user must enter a password, if it's wrong, they can try again. After a total of 3 tries the user logs off. I don't need help with the logging off part, but instead, I need help creating the looped part so the user has 3 tries before the log off is executed. I've tried using a do loop and IF statements but for the life of me I just can't seem to figure it out.


This is the code I have got so far, but, as you can see, it's not working, and I know I have messed it up, I just can't seem to figure the problem out.

 int count = 0;
           
                do
                {

                   if (PwordInput.text == "Password")
                   {
                   
                   Application.Exit();
            }

               else
               {
                    Console.Beep(2000, 500);
                    PwordInput.Clear();
                    count = count + 1;
               }
                } while (count < 3);

                if (count == 3)
                {
                    //Log off code
                }




I know the answer will be something simple (maybe using the wrong sort of loop?), but I just can't seem to figure it out, like I said, I'm new to C#, thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Looping and if statements

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 732
  • View blog
  • Posts: 1,878
  • Joined: 23-December 08

Re: Looping and if statements

Posted 28 May 2012 - 11:01 AM

here's my thought on the pseudo-code:


loggedIn = False;
for(int count=0; count < 3; count++)
{
    get the password
    if(password is correct)
    {
        loggedIn = True;
        break;
    }
    else
    {
        let the user know his entry was wrong. 
        optionally, you can tell him he has 2-count attempts left.
    }
}

if(!loggedIn)
{
    alert user to his failure and exit
}

proceed with program



This feels simple and clean to me, what do you think?

This post has been edited by atraub: 28 May 2012 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Looping and if statements

Posted 28 May 2012 - 11:11 AM

You never get the user to enter the password again after he entered the wrong password. You just clear the input and then check again whether the password is correct, which it won't be since you just cleared it.

I assume your code is in an event handler somewhere (presumably for a button click). You should only check the password once each time the handler runs and increase the counter each time - so no loop.
Was This Post Helpful? 2
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 732
  • View blog
  • Posts: 1,878
  • Joined: 23-December 08

Re: Looping and if statements

Posted 28 May 2012 - 12:06 PM

Haha, it's been so long since I've done event driven programming, I didn't even think of that. Good point sepp2k
Was This Post Helpful? 0
  • +
  • -

#5 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 178
  • View blog
  • Posts: 750
  • Joined: 27-October 11

Re: Looping and if statements

Posted 29 May 2012 - 02:12 AM

@OP

The thing is you're not using event driven programming. In .NET you have events which execute a specific block of code when some 'event' is triggered.

Look at this tutorial if you're interested more in events: Delegates, Lambdas And Events

So what you should do:

1. Use the Button_Click event for the logic of checking the input
2. If it fails increase the counter
3. Clear the inputs

Some pseudo-code:

int counter = 0;
btnSubmit_Click
{
   string userName = tbUserName.Text;
   string password = tbPassword.Text;
   if ( CheckUserInput(userName, password))
   {
      counter = 0;
      proceed();
   }
   else
   {
      counter++;
      tbUserName.Text = string.Empty;
      tbPassword.Text = string.Empty;
   }
}



There is one thing that is actually a preference: it's when you tell the user he used all 3 tries. If you want to tell the user after uses the 3 tries and then tries again (clicks the submit button for the 4th time) then you put

if (counter == 3)
  DoSomething();



at the start of the btnSubmit_Click event.

If you want to tell it to the user as soon as he uses the third try wrongly.

Then add something like:

if (counter == 2)
   DoSomething();



Just before the counter++;.
Was This Post Helpful? 1
  • +
  • -

#6 CDL2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-May 12

Re: Looping and if statements

Posted 29 May 2012 - 07:24 AM

Cheers Rex, that did the trick.
Thanks to everybody else as well :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1