Thread: Password Timeframe

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Password Timeframe

    Hi guys, just wondering how you would set a window for how long you have to enter a password, thanks!

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Your gonna have to be more specific than that. It is a bit difficult to tell exactly what you are trying to do or what in particular you are having problems with.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    If you mean to only allow the user to type in their password for a certain amount of time, look in to the SetTimer win32 function.

    http://msdn.microsoft.com/library/de...s/settimer.asp

  4. #4
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Use the functions in <ctime>

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    yes i want to let the user only have a certain amount of time to enter the password, i think ive seen a way to do it with Sleep(). Any ideas?

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Assuming you're doing a windows GUI application, read jverkoey's link.

    If you're doing a console application, you can use the nonstandard kbhit() which is defined in conio.h to check if a key has been pressed already, inputting the next character of the password if a key has been pressed and then check if it's correct.. etc. Put it in a loop, and on each iteration of the loop check if the time has elapsed. You can use GetTickCount() if you have <windows.h> included to find how long the system has been running in milliseconds, otherwise use the functions in <ctime>. An alternative to kbhit() that has been suggested by Anonytmouse in the past is to get a handle to the console input using GetStdHandle() and checking if input is waiting using WaitForSingleObject().
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Hi Guys, I went with the GetTickCount() approach. I need one minor bit of help though. It lets you enter the password for as long as you want. I know the problem and that is that the cin.getline() command has a cin.ignore() function built in anyone know how to get it out?

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        char password[25];
        int x=(GetTickCount()+3000);
        while(GetTickCount()<x)
        {
            cout<<"Password: ";
            cin.getline(password, 25);
        }       
        if(GetTickCount() >= x)
        {
            cout<<"DONE!";
        }
        if(strcmp(password,"password")==0)
        {
            cout<<"\nCorrect!";
        }
        else{
            cout<<"\nWrong!";
        }                
        cin.get();
        return 0;
    }
    **This is just a prototype program dont worry about how the password is hardcoded into the program

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
     DWORD start = GetTickCount();
         std::string pwd;
         cout << "Password: ";
         while(GetTickCount() - start < 10000) //10 seconds
         {
       if(kbhit())
    	  pwd += (char)getch();
         }
    Something like that should work.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    It wont compile, it says that kbhit() and getch() are undefined, that means the Header file is missing right? What header file is it under?

    PS- What is a DWORD?

    PSS- Any other ways?
    Last edited by Junior89; 01-12-2005 at 09:00 PM.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    kbhit() and getch() are in <conio.h>. You might not have it, since it isn't standard. Alternatively, you could write your own kbhit() function:
    Code:
     bool kbhit()
     {
       HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
       return (WaitForSingleObject(h, 0) == WAIT_OBJECT_0);
     }
    The functions I used in that come from <windows.h>, which it seems you do have.

    **EDIT**
    DWORD stands for 'double word', and is just a microsoft typedef for unsigned long. The function GetTickCount() returns a value of type DWORD, so for the sake of being absolutely sure that everything will be compatible (on the offchance that MS decides to make DWORD something else in the future), we store the result in a DWORD instead of unsigned long.
    Last edited by Hunter2; 01-12-2005 at 09:26 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A tenet connection from a C program..
    By ahmd2080 in forum Linux Programming
    Replies: 2
    Last Post: 07-04-2009, 03:42 AM
  2. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  3. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  4. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM