simple for loop problem

I'm writing a code where a user has 3 attempts to enter the correct password and I'm trying to make it so it shows:

Please enter your PIN (3) tries left:
Please enter your PIN (2) tries left:
Please enter your PIN (1) tries left:


Why is it that my j isn't decreasing by one?

1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < 3; i++)
	{
		int j = 3;
		cout << "Please enter your PIN (" << j << ") tries left): ";
		cin >> pin;
		j--;
		if (pin == u1.servicePin)
		{
			cout << "Access granted" << endl;
		}
	}
Last edited on
Your j decreases by 1. Then j is destroyed then a new one is created on the next iteration. You could simply do for(int i = 3; i >= 0; --i) or cout << "Please enter your PIN (" << 3 - i << ") tries left: "; Also, when you say Access granted you are probably going to want to break out of the loop.
Yes I know. Thanks for the correction.
Ok, now I'm stuck. Would I have to change my loop into a while or do-while loop to break out if the password is correct?
Inside of the if statement but after the output you would put break;

so
1
2
3
4
5
if(pin == u1.servicePin)
{
    cout << "Access granted" << endl;
    break;
}


http://www.cplusplus.com/doc/tutorial/control/
Towards the bottom of the link there is a section on the break statement.
Ok I have it working except my for loop ends when i type in the wrong password in the loop.

This is all of my code for the function.

How would i get it to keep on looping if it's wrong?

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
void loginPin()
{
	//variable

	string pin;
	string realPin;

	ifstream fin (FILENAME.c_str());
	while (getline(fin, u1.name) >> u1.phoneNumber >> u1.servicePin)
	{
	}
	cout << "Please enter your pin: ";
	cin >> pin;
	if (pin == u1.servicePin)
	{
		cout << "Access granted" << endl;
	}
	if (pin != u1.servicePin)
	{
		for (int i = 3; i > 0; --i)
		{
			cout << "Please enter your PIN (" << i << ") tries left): ";
			cin >> pin;
			if (pin == u1.servicePin)
			{
				cout << "Access granted" << endl;
				break;
			}
			if (pin != u1.servicePin)
			{
				cout << "Sorry, incorrect password." << endl;
				exit(0);
			}
		}
	}
}
remove line 32.

ps line 29 can be replaced with else
Last edited on
I would like the (main) program to completely end after all tries have been used up, but just finish to the end of the function if the password is correct. I don't know where to put exit(0); at.
Last edited on
never mind i got it.
There are a few things you could do the easiest would be to put an if before the exit(0) to check if i == 0. Alternatively make the i from line 20 local to the if statement before the for loop. Then after the for loop you would check if pin == u1.servicePin and if not then you would exit. The best solution would possibly be to return from the function weather the password is correct or not. Something like return pin == u1.servicePin then based on the return value you would exit or not after calling the function.
Yes. I just made the function return an int instead of being a void, and in the main program it would do whatever based on the return value.
Topic archived. No new replies allowed.