C++ Program/Code For Interactive Password requiring program

2 comments
//In this program, there is a protected information. A pre-defined password is required to reveal that.

#include <iostream>                       //For Input/Output Stream Related Functions
#include <conio.h>                         //For Console Input/Output function like clrscr & getche
using namespace std;                     //Using the "standard" namespace of iostream class
void main()
{
const int stored_pass=12376;        //making the per-stored password be "12376" & let it be a                                                           //constant so that it cant be changed during program run-time
int num_tries = 0;                          //Making the number of tries variable initialized by zero
int user_pass;
while (num_tries < 3)                    //keeping the max no. of tries of entry of password be 3


cout << "What is the password (You get 3 tries...)? ";
cin >> user_pass;                           //user inputs the password in order to reveal the secret information
num_tries++;                                 //once the password entered, the number of tries variable will also                                                          //be incremented.
if (user_pass == stored_pass)      //checking if the stored & the user entered password matches

cout << "You entered the correct password.\n";
cout << "The cash safe is behind the picture of the Madonna.\n";
break;                                            //If matched, then print the secret information on the screen
}
else                                                //If not matched then retry the password entry

cout << "You entered the wrong password.\n";
if (num_tries>= 3)                      //If the number of tries is equal to 3

cout << "Sorry, you get no more chances"; 
}
else                                              //If not matched & num_tries<3 then displaying how many tries left                

cout << "You get " << 3-num_tries <<" more tries...\n";
}
}
}
getche();                                     //To exit the program after an user's keystroke

}

2 comments:

Unknown said...

nice but it gives me a massage that the "main" must return the "int".

Vibhutesh said...

SUDI MOHAMED: its just a warning!
But if wanna avoid it, just use int main() instead of void main() and just after getche() function, add return 0;