简体   繁体   中英

Implementing a timer in c++

I am currently making a game in c++. I have below a timer function:

void timer(int &s , int &m , bool a)
{
   while (a)
   {
      Sleep(1000);
      if (s%60)
        m+=1;
      s+=1;
   }
}

Here is the function for the game:

void game()
{
   int s = 0 , m = 0;
   char a[]="Computers";
   char b[10];
   timer(s,m,true);
   while (strcmpi(a,b)!=0)
   {
      cout<<"Guess the word:";
      gets(b);
   }
   timer(s,m,false);
   cout<<"You got it correct!\n";
   cout<<"Time taken : "<<m<<':'<<s<<endl;
}

When I run the program , nothing happens. I am guessing the timer is running and not allowing the while loop in the game() to get executed.

So basically I am trying to find out the time the user takes to guess the word correctly.

How can I overcome this problem?

Thanks in advance :)

You can use the chrono functions:

#include <chrono>

using namespace chrono;

auto a = high_resolution_clock::now();

... what you want to measure ...

auto b = high_resolution_clock::now();

cout << "took " << duration_cast<seconds>(b - a).count() << << " seconds" <<  endl;

Your code will block on while(true) . Please consider using std::chrono .

void game()
{
   int s = 0 , m = 0;
   char a[]="Computers";
   char b[10];
   auto start = chrono::steady_clock::now();
   while (strcmpi(a,b)!=0)
   {
      cout<<"Guess the word:";
      gets(b);
   }
   auto time_elapsed = chrono::steady_clock::now() - start;
   cout<<"You got it correct!\n";
   cout<<"Time taken : " << std::chrono::duration_cast<std::chrono::seconds>
                         (time_elapsed ).count() << " s"<<endl;
}

If all you want is the time taken, chuck your timer. Ask what time it is when the user starts, store it, and subtract the start time from the time when the user finishes.

void game()
{
   int s = 0 , m = 0;
   char a[]="Computers";
   char b[10];
   time_t start = time(NULL); <<<< Modification here
   while (strcmpi(a,b)!=0)
   {
      cout<<"Guess the word:";
      gets(b);
   }
   cout<<"You got it correct!\n";
   time _t taken = time(NULL) - start;
   m = taken / 60; <<<< Modification here
   s = taken % 60; <<<< Modification here
   cout<<"Time taken : "<< m <<':'<<s<<endl;
}

If allowed by the assignment, consider replacing char sa and b with string sa and b. The user cannot overflow the string and wreak havoc on your program the way they can with char arrays and gets .

void game()
{
   int s = 0 , m = 0;
   std::string a ="Computers"; <<<< Modification here
   std::string b; <<<< Modification here
   time_t start = time(NULL); 
   do
   {
      cout<<"Guess the word:";
      cin>> b; <<<< Modification here
   } while (a != b); <<<< Modification here. No sense testing b before you read b
   cout<<"You got it correct!\n";
   time _t taken = time(NULL) - start;
   m = taken / 60; 
   s = taken % 60; 
   cout<<"Time taken : "<< m <<':'<<s<<endl;
}

You can use the time.h header file to measure time elapsed.Your code can b like this:

#include<time.h>
void game()
{ 
 int s = 0 , m = 0;
 char a[]="Computers";
 char b[10];
 clock_t start, end;
 double time_used;
// timer(s,m,true);  comment out this line
 start = clock();
 while (strcmpi(a,b)!=0)
  {
  cout<<"Guess the word:";
  gets(b);
  }
  end = clock();
 time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
 timer(s,m,false);
 cout<<"You got it correct!\n";
 cout<<"Time taken : "<<time_used<<" seconds"<<endl;

}

For more information on time.h header file see this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM