timer in a small program

i want to show a question with count down...
but not getting this logic
help me experts...

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
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
int x,y,z,a;
x=rand();
y=rand();
z=x+y;

for (int i=60; i>0; i--)
{
cout <<"Time Remaining : "<<i;
Sleep(1000);
cout<<x<<" + "<<y<<"=";
cin>>a;
if(z==a)
{
cout<<"right answer";
break;
}
system("cls");

}
getch();
return 0;
}
instead of cin, try

a = get();

you'll need to include <istream>
Last edited on
Hi there,

If you want to keep this simple, you will have to do something like this:

1
2
3
4
5
6
7
8
9
const int seconds = 10;
int waiting_time = seconds * 1000; 

while (waiting_time >= 0)
{
    waiting_time-=1000; //decrease time by one second
    sleep(waiting_time);
    std::cout << waiting_time / 1000 << " seconds left." << std::endl;
}


It loops the sleep() function for every second.

Hope that helps.

All the best,
NwN
Last edited on
i did according to ur suggestion bt not getting timer in my problem..

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
 #include <iostream>
#include <windows.h>
#include <conio.h>
#include<istream>
using namespace std;
int main()
{
int x,y,z,a;
x=rand();
y=rand();
z=x+y;

for (int i=60; i>0; i--)
{
cout <<"Time Remaining : "<<i;
Sleep(1000);
cout<<endl;
cout<<x<<" + "<<y<<"=";
a=get();
if(z==a)
{
cout<<"right answer";
break;
}
system("cls");

}
getch();
return 0;
}
Topic archived. No new replies allowed.