trying to get a timer in a game i made, can't get it to work.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cstdlib>
using namespace std;
int t=0; //timer
int main()
{
  int r; //random number 
  int x; //input for random number
  char a;//play again
  int s; //score
  s=0;
  a='y'; //sets play again to yes 
  int h; //picks between letter and number
  int to; //starting time
  int tf; //ending time
  char y;
  cout<<"This program will test your reflexes.\nSimply copy the number or letter shown as fast as you can.\n but be careful, you only have 1 seccond to answer.\n If you can survive for 60 seconds, you win."<<endl;
  while (a=='y')
  {
  h=(rand() % 3);
  if (h==2)
  {
    h=(rand() % 3);
    r=(rand() % 10);
    cout << r << "\n";
    to=t;
    cin >> x;
    tf=t;
    if (x!=r || (x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=6 && x!=7 && x!=8 && x!=9 && x!=0) || tf-to>1)
    {
      cout <<"FAILURE your score was "<<s<<endl;
      cout <<"\ntry again? (y/n)"<<endl;
      s=0;
      cin>>a;
    }
      else
      {
        if (t<60)
        {
        s=s+1;
        cout <<"\nyour current  score is "<<s<<endl;
        cout <<"time left:"<<60-t<<endl;
        }
        else      
 {
        s=s+1;
        cout <<"\nyou win. Your final score is"<<s<<endl;
        }
      }
  }
   else
     {
       h=(rand() % 3);
       char ch = 'a' + rand() % (('z'-'a') + 1);
       cout << ch << "\n";
       to=t;
       cin >> y;
       tf=t;
       if (y!=ch || tf-to>1)
         {
           cout <<"FAILURE your score was "<<s<<endl;
           cout <<"\ntry again? (y/n)"<<endl;
           s=0;
           cin>>a;
         }
           else
             {
              if (t<60)
               {
               s=s+1;
               cout <<"\nyour current  score is "<<s<<endl;
               cout <<"time left:"<<60-t<<endl;
               }
                 else
                   {
                   s=s+1;
                   cout <<"\nyou win. Your final score is"<<s<<endl;
                   }

             }
     }
  }
return 0;
}
int time() //increases t by one every second
 {
  for(;;) 
  {
   t++;
   sleep(1);
  }
  return 0;
}
Last edited on
1) You are not calling your time() function anywhere
2) If you call it it will hang your program.

You either should compare time points made in main loop and use asynchronous input, or create multithreaded application
Last edited on
Topic archived. No new replies allowed.