C++ cin Prompt for a password and check it against an internal one

Description

C++ cin Prompt for a password and check it against an internal one

#include <iostream>
using namespace std;
#include <stdlib.h>
int main()//from www.  j a  va2  s.co  m
{
   int stored_pass = 11862;
   int num_tries = 0;     // Counter for password attempts.
   int user_pass;
   while (num_tries < 3)              // Loop only three times.
   {
      cout << "What is the password (You get 3 tries...)? ";
      cin >> user_pass;
      num_tries++;                   // Add 1 to counter.
      if (user_pass == stored_pass)
      {
         cout << "You entered the correct password.\n";
         cout << "The cash safe is behind the picture " << "of the ship.\n";
         exit(0);
      }
      else
      {
         cout << "You entered the wrong password.\n";
         if (num_tries == 3)
         {
            cout << "Sorry, you get no more chances";
         }
         else
         {
            cout << "You get " << (3-num_tries) << " more tries...\n";
         }
      }
   }                               // End of while loop.
   exit(0);
   return 0;
}



PreviousNext

Related