ENTER CORRECT PASSWORD, THREE ATTEMPTS ONLY USING C++ WHILE-IF COMBINATION

Before I proceed, I want to give thanks to my viewers in YouTube as this is so far, the 'most viewed' video presentation that I created. (At this date, there is already 1,009 views (dated Nov, 30, 2021).

Check this out... YouTube   ENTER CORRECT PASSWORD, THREE ATTEMPTS ONLY USING C++ WHILE-IF COMBINATION

There is one request from the YouTube 'comment box' regarding getting the source code. I will give it to all you at the end part of this blog but for the sake of those  beginners and others who wish for me to explain each sections, it will be my great pleasure to give them in details.

First - let's discuss what is the what-I-call 'WTD' or "WHAT-TO-Do" dialog.

The cout<< is a C++  program syntax to 'display something' on the 'USER'S CONSOLE' before the blinking cursor appears.
.
Let's consider this as a C++  'command code'. 

The main idea here is to give the user (not the programmer), an idea of what to do. It could be a request along with an instruction or other additional information. One example is...
 
                                  cout<<"ENTER THE CORRECT PASSWORD \n";
                                  cout<<"Clue: Common greeting then count 1 to 3 \n";
      
                                  cout<<"Enter password: ";

Keep in mind that for every WTD, there is a C++ syntax - cin>> that must 'follow'. This can be considered as a command code to 'enter something' somewhere inside a computer and this represented by a BLINKING CURSOR in C++ that we see on the User's Console. But cin>> cannot stand alone without putting with it a direction of where to put the 'input'. This is how important 'containers' are. 

Consider the example below:
      
                                   cin>>ps;

CONTAINER, as its name signify -  means a thing that can carry, store or contain 'something'. A 'bottle' is one good example of a container that keeps a certain liquid inside it. In C++, a 'container' can also considered a 'memory location' that can store data.  

(I will give a separate topic regarding specifics about containers....later).
 
In this created program, the cin>> command code is directed to store data in a container with label or name 'ps'.

So, keep in mind that it is 'best practice' to declare first 'containers' before the actual program coding. 
So, we have the following:

                                  string PW = "Welcome123";
                                  string ps;
                                  int i = 0;
                                  int n = 1+i;

The syntax - [ string PW = "Welcome123"; ] is considered as a 'FIXED CONTAINER' as we already put value on it during program coding.

But the syntax - [  string pw; ] is a 'FLEXIBLE CONTAINER'. It is flexible, in a sense that we can 'change' its content based on what the cin>> command code wanted to put in it.

The [  int i = 0  ] is particular for counting the number of attempts

However, the [ int n = 1+i; ] is optional and will only display the number of attempts that goes along with 'Dialog Response', " Attempt # ".

Notice that there are instances that we need to 'initialize' the 'values' as we declare 'variables' (or memory locations).  This is very important.

NEXT, the WHILE PROCESS ( I rather call it a process as the 'statement' only means the numerous syntax inside the WHILE's curly brackets { and } ) will act on what to do about the containers that we declared.

                                    while (PW!=ps, i<3)
                                         {  cout<<"Attempt#"<<n<<endl;
                                             cout<<"Enter password: ";
                                             cin>>ps;
                                              i++;
                                              n++;   }
                                         
Always remember, that a WHILE process always need three things:

       1.  a  'starter'.
       2.  a 'condition'.
       3.  and a 'stopper'

(I will again, give a separate topic regarding specifics about WHILE, FOR and IF-ELSE PROCESSES... in the coming days)

In this program coding, we actually have two starters. The first one is the word "Welcome123" and the other one is to start the interval  ' i ' at 'zero'.

LET'S HAVE AN EXPERIMENT:

Try to delete the '= Welcome123' in the syntax  [ string PW = "Welcome123"; ].
Next, put back the '= Welcome123'  and try to delete the value zero (including the equal sign) in the syntax [  int i = 0; ] .
By the way, don't forget the semicolon '  ;  ', so you'll not get an error message.
SEE what will happen...

Next, the CONDITION that as long as the input is not equal to "Welcome123", the WHILE process will simply continue to loop back.  

But the thing is, it will give a user the chance to be able to guess the password, as there's no limit to how many passwords that can be entered.

To avoid that scenario (and as I discovered it that there is  a possibility to include another condition) we have to include another condition - to limit the 'input attempts' (the label or name -  ' i ' can be considered to signify 'input'). So the condition will be, as long as the 'input attempts are less than three...
                             
                                                    while(PW!=ps, i<3) 
         
Next, the STOPPER. In this C++ coding, there are two exisitng 'stoppers'. The first stopper is of course, if we enter the exact word "Welcome123" with capital (or uppercase) 'W'. Take note, it should be exactly the same or it will be rejected, as the condition says... 
                                                     "  PW = ps  "
The second stopper is the number of 'input attempts'. Don't forget to include the 'incremental function' for ' i ' :
                                                         i++;

LET'S HAVE AN EXPERIMENT:

Try to delete the i++;  and see what will happen...

As analysis - for the first cycle, i = 0 as we initially declared it.  Since input attempt is not yet equal to 'three', it will loops back and BEFORE it loops back, we add 1 to ' i ' so it becomes  i = 1  and so on until input attempt is equal to three. Without the function of adding 1 to the value of i, (as we forget to put   i++; ), what will get is an never-ending 'garbage information' displayed on the user's console. 

BUT THE PROBLEM is - what if  we actually 'hit' the right password on the first attempt ? If we enter the password "Welcome123 on the first try, it will still loops back as we not yet 'satisfied' the second condition  i<3 . Only if  i=3  that it will jumps out of the WHILE process to do the next 'task'.

To avoid that, we need to include an IF-ELSE PROCESS 'inside' the WHILE loop. That is, the IF statement and the ELSE  statement are still part of the WHILE process. Notice that I call each one of them as  'statement' if they are individually represented as it has no process to consider,  it simply a statement that only choose either true or false. They become a process, if in case, we include the ELSE statement and do a task ( as in other C++ program designs, sometimes we don't include the ELSE statement).

                                          while (PW!=ps, i<3)
                                              { // There are 'tasks' inside here before the IF statement 
                                                  if  (PW!=ps)
                                                     { cout<<"Try again...\n";
                                                      cout<<endl; }
                                                  else {ps="Welcome123"; i=3;} 
                                               }
                                       
This is where the IF statement and ELSE statement got the 'greatest impact' on this program design.

The IF statement only give 'assurance' that the process will simply loops back as we enter a wrong password. But the ELSE statement works like a 'counter-act' initiator - as it AUTOMATICALLY changes the values for ps and ' i ' that will satisfy the WHILE process' 'conditions' and will jump out of the WHILE process to the next task.

LET'S HAVE AN EXPERIMENT:

Try the above WHILE process without the IF-ELSE process.

Then compare to the next WHILE process with the IF-ELSE process included.

The WHILE process will give two possible outcomes - either PW= ps , that is the user was able to 'hit' and entered the correct password or 'sorry to say', PW != ps, or up to the third attempt, failed to enter the correct password.

The last thing to do is to include another IF-ELSE process. Together, they will act as  'AUTO SWITCH"

                                             if (PW==ps)
                                                {cout<<"proceed...";}
                                          else{cout<<"Sorry, Try later";}

The switching process toggles to either to 'allow' the user to proceed or  to 'deny'  to proceed. 
The two dialog, "proceed" and "Sorry, Try later" can be considered as 'Response Dialog' and
of course, without this IF-ELSE process to separate the two possible outcomes, this program coding will be incomplete. 

And now... for those who are requesting for the 'source code', here it is...

Designed by sirjon ( C++ works )

01  #include<iostream>
02  using namespace std;
03  int main ()
04  {   cout<<"ENTER THE CORRECT PASSWORD \n";
05      cout<<"Clue: Common greeting then count 1 to 3 \n";
06      cout<<endl;
07
08    string PW = "Welcome123";
09    string ps;
10    int i = 0;
11    int n = 1+i;
12
13       while(PW!=ps, i<3)
14         {cout<<"Attempt#"<<n<<endl;
15          cout<<"Enter password: ";
16          cin>>ps;
17          i++;
18          n++;
19          if(PW!=ps)
20           {cout<<"Try again...\n";
21            cout<<endl;}
22          else {ps="Welcome123"; i=3;}
23          }
24       if(PW==ps)
25        {cout<<"proceed...";}
26       else{cout<<"Sorry, Try later";}
27      
28     return 0;
29  }

NOTES TO CONSIDER: 

Possible Modification to follow:

Simply delete...
   08    string PW = "Welcome123";

and then change the following into...
   13       while(ps="Welcome123, i<3)
   19          if(ps="Welcome123, i<3)
   24       if(ps=="Welcome123)

THAT'S ALL FOLKS for now...
Have a good day and keep safe!

Comments

  1. Someone suggested that it is 'no longer necessary' to include in the While Condition the
    syntax - PW!=ps.
    Same way, to remove - ps= "Welcome123"; on the Else Statement.

    Thank you, Salem.

    ReplyDelete

Post a Comment

Popular posts from this blog

C++ SOURCE CODE FOR SIRJON'S "MICRO" TIC TAC TOE GAME FOR FUN!

C++ SOURCE CODE FOR SIRJON'S "MICRO" TIC TAC TOE GAME FOR LAPTOPS