display a string for 3 seconds and then redirect

If the user enters a wrong password I want the "wrong password" message to flash on the screen for like 3 seconds and then direct the user to the access() function..so that he can try again.


Is there any way to do that?



This is my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  void access()
{
    cout << "W E L C O M E   T O   T H E   L I B R A R Y   D A T A B A S E" << endl;
    cout << "-------------------------------------------------------------------------------" << endl;
    
    cout << "Enter your password: " << endl;
    string adminpass;
    if (adminpass == "admin")
    {
        adminmain();
    }
    else
    {
        cout << "Wrong Password! " << endl;
        // Go to a screen with wrong password message and then display the function again.
        
    }
Last edited on
@omkarborkar95
Here's one way. You may have to fiddle with it a bit because I didn't test it, just added it to the code you've already listed.
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
void access()
{
 bool ok = false;
 string adminpass;
 int ck = 0;
 cout << "W E L C O M E   T O   T H E   L I B R A R Y   D A T A B A S E" << endl;
 cout << "-------------------------------------------------------------------------------" << endl;
 while( !ok )
 {
	cout << "Enter your password: " << endl;
	cin >> adminpass;
	if (adminpass == "admin")
	{
	 ok = true;     
	 cout << "Password accepted!!" << endl;   
	Sleep(2000); // Needs #include <Windows.h> 
       adminmain();
	}
	else
	{
	 cout << "Wrong Password! " << endl;
	 ck++; // Give user only 3 tries to enter correct password
	 // Go to a screen with wrong password message and then display the function again.
	 Sleep(3000);// waits for 3 seconds
	}
	if(ck==3)
	{
	 cout << "You failed to enter correct password!" << endl;
	 cout << "Program will now terminate!" << endl;
	 Sleep(3000);
	 return; 
	}
 }
} 
}
Yes! its executing perfectly.
The sleep(2000) kept the message for about 2000 seconds.
So I used sleep(2) instead.

Where do i learn all this( like sleep() function etc)?
This is so cool. I generally refer to c++ primer plus for any problems and bugs.
Any books u recommend for learning these things.

btw i use a mac so pl don't suggest windows related books.
Thank You so much!
Sleep(2000) correctly waits for 2 seconds, see Microsoft documentation:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx


Note that sleep() is a UNIX API and has different implementation. Of course, this is the right API to use if you are on Mac.
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/sleep.3.html
Thanks!
Topic archived. No new replies allowed.