Thread: Two conditions for 'while' statement is valid

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    3

    Two conditions for 'while' statement is valid

    Hi, my name is Sirjon and I'm glad that Cprogramming Forum accepted me as part of this group.

    I ran into this old thread with the title "Password check with 3 attempts" started by 'samwillc' way way back in 2012.
    (The date was 6-9-2012) and I found myself to be with the same situation as to what samwillc 'experienced'. For being a beginner and don't really have formal training in C++ and just doing this as a hobby - I was challenged to solve this puzzle and told myself I will not stop until I GOT IT!
    For days, I worked on it and finally, I solved it by finding the 'missing links'.
    First, I realized (or discovered by myself), that it is possible to have two conditions for WHILE statement at the same time.
    So I discovered - the WHILE 'loop' continues as long as...
    1. The password is not the same password stored in container PW (with datatype string)
    2. the i (which represents the number of attempts) is less than three (as initially, int i= 0 )

    Here it is my C++ program design...

    Code:
    { // part of it...
    
    while(PW!=ps, i<3)
          { cout<<"Attempt#"<<n<<endl;
             cout<<"Enter password: ";
             cin>>ps;
             i++;
             n++;
          }
    But I realized there are more problems to consider;

    First - although one already entered the correct password, the loop still continue until i=3 before it proceed to the next task. No matter if the password is 'right' it will only pass through on the third attempt.

    Second: Although you entered three wrong passwords, automatically, this program will still allow to proceed to the next task

    Including a combined IF-ELSE statements inside the WHILE loop SOLVED the first problem...

    Code:
     // simply part of the whole program
    
       while(PW!=ps, i<3)
          {cout<<"Attempt#"<<n<<endl;
            cout<<"Enter password: ";
            cin>>ps;
            i++;
           n++;
        if(PW!=ps)
          {cout<<"Try again...\n";
          cout<<endl;}
        else
          {ps="Welcome123"; i=3;}
       }
    The IF statement will simply 'loops back' the WHILE cycle if one entered a wrong password.
    But with the ELSE statement setting 'ps' to Welcome123, which what is stored inside PW and setting i=3, then it gives a tremendous IMPACT that will automatically jumps out of the WHILE cycle and go to the next task.

    To solve the second problem, simply add another IF-ELSE statement outside and after the WHILE LOOP ...

    Code:
     // just a part of the whole program...
    
                if(ps==PW)
       {cout<<"proceed";}
                else {"Sorry, Try later";}
    return 0;
    }
    I uploaded in YouTube a similar video presentation of it but I'm not sure if the Admin will allow me to give the title (rules are rules that we have to follow). So rather, if there are beginners and amateurs out there like me - this is simply my way of sharing my little knowledge. It is true, these experiences and discoveries I'm getting from C++ programming makes me feel excited and fulfilled every time I accomplished my goals. That's why I keep saying, "C++ works!
    Last edited by C++bySirjon; 12-02-2021 at 05:25 AM.

  2. #2
    Registered User
    Join Date
    Nov 2021
    Posts
    3
    Pardon folks - I kept getting an error several times when I was trying to post this thread. At last, I got it. I realized not to put a tag on it all.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,152
    The comma operator doesn't do what you think it does.

    while(PW!=ps, i<3)
    is just
    while( i<3)
    which is why "No matter if the password is 'right' it will only pass through on the third attempt."

    Perhaps try it with
    while(PW!=ps && i<3)
    which will loop while both conditions are true.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2021
    Posts
    3
    Yes. You're right. I checked and tried it. There's no need to include the PW!=ps as part of the While condition.
    So, the IF-ELSE combo really the most 'instrumental' part to include inside the While statement to be able to achieve the goal. Same way, to remove

    ps="Welcome123"; on the ELSE statement

    It makes the program simpler.

    Thanks.
    Last edited by C++bySirjon; 12-03-2021 at 08:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statement multiple conditions
    By Jack Gell in forum C Programming
    Replies: 1
    Last Post: 01-07-2018, 09:10 PM
  2. if statement with 2 conditions
    By robando in forum C Programming
    Replies: 2
    Last Post: 04-12-2015, 04:18 AM
  3. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  4. IF statement - to conditions
    By cornacum in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 08:39 PM
  5. If statement conditions
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:55 AM

Tags for this Thread