Program jumping?

I have this program that I put in the startup folder of my computer, it is a password program. I made this program a few hours ago. I have a problem, everytime, it says you have 3 attempts, 2, then it jumps to 0. Here is my coding

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
55
56
57
58
59
60
61
62
//-----------------------------
#include <iostream>
#include <windows.h>
#include <string>
#include <ctime>

using namespace std;
//-----------------------------
void Delay( int secs )
{
  clock_t start = clock();
  clock_t end = clock();

  while( ( end - start )/CLOCKS_PER_SEC < secs )
  {
    end = clock();
  }
}

int main()
{
	int r = 0, f = 3;
int confirmexit = 0;
	RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
	//Password that user types in
    string password ;
    const char correctpassword[] = "family";

	cout << "Enter Password: ";
    cin >> password;
    cout << "You typed " << password;

    system("pause>nul");
	cout << endl << endl << "Checking Password";
	cout << endl << "Press any key to continue";
	system("pause>nul");
    if(password == correctpassword)
    {
        cout << endl << endl << "Correct Password" << endl << endl;
    }
    else
    {
        while(password != correctpassword)
        {
			if(r == 3)
			{
			cout << "Too many failed attemps, you can enter your password in 5 minutes";
			Delay( 300 );
			f = 0;
			}
            cout << endl << endl << "Incorrect Password, you have " << f << " Attempt(s) left";
            cout << endl << "Please type it again: ";
            cin >> password;
			r += 1;
			f -= r;
            if(password == correctpassword)
            {
               cout << endl << "Correct Password";
            }
		}
	}
}
1
2
3
4
5
			if(r == 3)
			{
			//...
			f = 0;
			}

Last edited on
You have f -= r; where I would expect to see a simple f--;

An unrelated comment. Function Delay() is resource-hungry. It is greedily consuming CPU cycles during the delay period. It would be a good idea to add a Sleep() call within the loop, in order to release the processor for other tasks.
Thanks
@Chervil
I did the Delay to make it OS independent, Sleep is OS Dependent
Topic archived. No new replies allowed.