Passcode Lock

I need help writing a program that allows the user to input a 4 digit password. The correct password is hard coded "1234", after 3 incorrect attempts it should display " the phone is locked" and break. Please help with my while loop and elif!

#include <iostream>
using namespace std;
int main()
{
char response;
int passcode = 1234;
int input;
int lock = 3;
int limit = 0;


cout << "Welcome user!" << endl;
cout << " Please enter your passcode: " << endl;
cin >> passcode;


if (passcode == input)
{
cout << "Password was correct, Welcome!" << endl;
}
else if
{
while (passcode != input)

cout << " Incorrect passcode, You have " << lock-- << "Trys remaining" << endl;

if (lock == 0)
cout << " Too many tries mother. " << endl;
break;
}
cin >> response;
return 0;


}


Edit: Please edit your post and always use code tags like I did with my code - http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool notIncorrect = true;
int attempts = 0;
while(notIncorrect) // runs as long as notIncorrect == true
{
    if (passcode == input)
    {
        cout << "Password was correct, Welcome!" << endl;
    }
    else 
   {
        attempts++;
        if(attempts == 3)
        {
             notIncorrect = false;
        }
   }


Not the best way but, did this super fast ^^^It works for now =)
Last edited on
You have a small mistake on the cin >> part, you want the int passcode = 1234 not to change and you have set cin >> passcode, which after the user enters the passcode will change the value of passcode, and then it will be messed up which will make the program malfunction. Here's the fix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
int main()
{
char response;
int passcode = 1234;
int input;
int lock = 3;
int limit = 0;


cout << "Welcome user!" << endl;
cout << " Please enter your passcode: " << endl;
cin >> input;


if (input == passcode)
{
cout << "Password was correct, Welcome!" << endl;
}
else if
{
while (input != passcode)

cout << " Incorrect passcode, You have " << lock-- << "Trys remaining" << endl;

if (lock == 0)
cout << " Too many tries mother. " << endl;
break;
}
cin >> response; //I can't seem to figure out what you wanted to do on this line, response isn't even declared, safe to delete.
return 0;


}
I saw your post and as a beginner I decided to create a passcode program as well! here is mine. I had a lot of fun with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	
//passcode
#include <iostream>
	using namespace std;

	int main()
{
		int passcode = 1234;
		int input;
		int tries = 3;

		cout << "Enter passcode";
		cin >> input;


		//passcode loop
		if (input == passcode)
			cout << "Success!\n";

		else if (input != passcode)        //You need a condition for your else if statement

		{
			while (input != passcode)
			{
				cout << "Sorry try again, you have " << tries-- << " left.\n";
				cin >> input;

				if (tries == 0 && input !=passcode)
				{
					cout << "Sorry you have exceeded the maximum attempts\n";
			
					return 0;
				}

				else if (tries == 0 && input == passcode)    // this ensures that the program recognizes the correct input even on the last attempt otherwise I found that my program just ends if I don't add this.

				{
					cout << "Success!\n";
				}

				else if (input == passcode) // I found that if I don't have this then the program does not work if you input the correct passcode on any attempts other than the first one.
				{
					cout << "Success!\n";
				}
			}

		}

		return 0;
		



}
Topic archived. No new replies allowed.