ways to stop a running clock

hi everyone
i wonder, is there anyway to stop an unfinite loop of a running time

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
#include<iostream>
#include<windows.h>

using namespace std;

void gotime(int&, int&, int&);

int main()
{
    int h, m, s;

    h = m = s = 0;

    while(true)
    {
        Sleep(1000);
        gotime(h, m, s);
    }
}

void gotime(int& h, int& m, int& s)
{
    if (s >= 60)
    {
        s = 0;
        m++;
    }

    if (m >= 60)
    {
        m = 0;
        h++;
    }

    if (h >= 24)
        h = 0;

    s++;

    system("cls");

    if(h < 10)
        cout << "0" << h << ":";

    else
        cout << h << ":";

    if(m < 10)
        cout << "0" << m << ":";

    else
        cout << m << ":";

    if(s < 10)
        cout << "0" << s << endl;

    else
        cout << s << endl;
}
Use the new CRAY supercomputer. It can calculate the infinite loop in 10 minutes!


No, joking. use "break" inside the loop. If you want to cancel from within "gottime", you can use the return value of gottime:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
    while(true)
    {
        Sleep(1000);
        if (gottime(h,m,s))
            break;
    }

void gotime(int& h, int& m, int& s)
{
...
    if (h == 23 && m == 23 && s == 23)
        return true; // time of the illuminati! Break immediately!
...
    return false;
}


Ciao, Imi.
Last edited on
....then what about if the user wants to stop the time when the user hit anykey in the keyboard, is that possible to stop the time??
thanks Duoas,
i had a question about the code below, what does this function actually does?

1
2
3
4
5
6
7
bool iskeypressed( unsigned timeout_ms = 0 )
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    timeout_ms
    ) == WAIT_OBJECT_0;
  }
It does exactly what I said it does in the thread you got it from. Try running the example program to see it working.
Topic archived. No new replies allowed.