0% found this document useful (0 votes)
42 views3 pages

C Tutorial

The document describes a C++ program that allows a user three attempts to enter a password to open a door. It prompts the user to enter the password, checks it against the correct password, and increments the attempt number. If the password is incorrect after three attempts, it outputs that the door is locked.

Uploaded by

Abbas Kimolo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

C Tutorial

The document describes a C++ program that allows a user three attempts to enter a password to open a door. It prompts the user to enter the password, checks it against the correct password, and increments the attempt number. If the password is incorrect after three attempts, it outputs that the door is locked.

Uploaded by

Abbas Kimolo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question:

Using C++, write a program that will OPEN (output on terminal a word OPEN) a door to a user
after keying in a right key combination(say 1234). Your solution should give three (3) trials to
the user, if wrong 3 times, the door should LOCK (output on terminal a word LOCKED). On
wrong password entry, the user should be prompted to try again and the number of trials he/she has left.

ANSWER :
A program code
/*NAME: KITO
REG NO: 2003061
INDIVIDUAL ASSIGMENT NO 01
A program to allow the user entering password to open the door,
when he/she enter wrong password 3 trials the door lock Automatic*/
#include <iostream> //Header
using namespace std;
int main() //Main function
{
string psw1 = "1234",psw2;
int attemptno = 1 ;
cout << "Please enter password to OPEN the door: ";
cin>>psw2;
while(psw2 != psw1 && attemptno<=3){
attemptno++;
cout << "Wrong Password(you remained with "<< 5-attemptno<<" trials) Please
enter correct password: ";
cin>>psw2;
}
if ( psw2 != psw1 && attemptno !=3 ){

1
cout << "door LOCKED";
}
else {
cout << "door UNLOCKED";
}
return 0;
}
I. When entering the correct password on the first attempt the output will be

II. When entering the wrong password on the first attempt and then entering correct
password on the 2nd attempt the output will be

III. When entering the wrong password on the first and 2nd attempts, and then entering
correct password on the 3rd attempt the output will be

2
IV. When entering the wrong password on the 1st, 2nd & 3rd attempts, and then entering
correct password on the 4th attempt the output will be

V. When entering the wrong password on all attempts the output will be

You might also like