timer in c++?

This is a discussion on timer in c++? within the C++ Programming forums, part of the General Programming Boards category; Another little program/game I'm working on in c++ is, how many times can you press the 'Enter' key in 20 ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    timer in c++?

    Another little program/game I'm working on in c++ is, how many times can you press the 'Enter' key in 20 seconds. I've made it output a higher number every time you press enter, but I haven't made the timer. How do you do that?
    Here's the code I have allready.

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    int press = 0;
    
    for(press = 0; press < 1000; press++)
    {
    cin.get();
    cout << press << endl;
    }
    return 0;
    }
    How do I get the timer in this code?


    Greatfaith.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A na´ve start might be like this.
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
       int press = 0;
       time_t now, start = time(0);
       while ( (now = time(0)) < start + 20 )
       {
          cin.get();
          cout << ++press;
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 12-29-2005 at 09:37 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    ...and as I'm sure Dave knows the problem with his "naive program" is that you could just let it sit there never pressing anything and it will never end. I would suggest using conio.h and kbhit()

    Code:
    #include <iostream>
    #include <ctime>
    #include <conio.h>
    
    using namespace std;
    
    int main() {
       int press = 0;
       time_t now, start = time(0);
       
       while ( (now = time(0)) < start + 20 ) {
          if (kbhit()) {                           // Wait for input
            if (getch() == 13) {                   // Compare that input to Enter
               cout << ++press << endl;
            }
          }
       }
       
       cout << "You hit ENTER " << press << " times.";
       cin.get();                   // You should put this in a check to see it isn't
                                    // ENTER otherwise you pound your key into
                                    // closing the program
       return 0;
    }
    Last edited by SlyMaelstrom; 12-29-2005 at 10:22 PM.
    Sent from my iPad«

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    If you want to assume that the OP is using an OS/Compiler which supports such functionality....

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    How do I put in a check to see if another key is pressed, other than ENTER?

    cin.get(SPACE); ??????

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The get() function doesn't return as soon as any key is pressed. It returns when enter is pressed, and you can press whatever you want before that. If you want a function that will return as soon as any key is pressed, you're going to have to go outside the standard. The most popular method is found in conio.h, which is not standard but it is common.

    Code:
    #include <conio.h>
    ...
        char mychar = getch();
    mychar will then hold the character that was pressed. If the person pressed space it will contain, " ".

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    15
    hmm i knew it went something like this
    Code:
     
    #include <time.h>
    #include<stdio.h>
    int main(void){
     long start,end;
      start=clock();
      do
         end=clock();
      while((end-start)/CLK_TCK<20);
    }
    to convert into seconds the time spent is divided by CLK_TCK which is defined in <time.h> header ...uhm it's kind of outdated but hopefully you'll still have it in your library check it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21