How do i make a function stop and resume in a cetain number of time?

Okay i made a program that says hello world in a txt file and i want it to display the time in the the file every minute.I succeeded in doing that the only problem in the while statement i have a Sleep(100) so every 100 mili seconds it loops and it keeps displaying the time.I want it to display the time once then 2 min later say the time again but while thats happening keep saying hello world.I used the sleep but when i used Sleep(), it pauses the typing of hello world which i dont want any help?

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
  #include <iostream>
#include <time.h>
#include <windows.h>
#include <fstream>
#include <sstream>
#include <cstdlib>



int Time(time_t rawtime);
void hello();

using namespace std;

int main()
{
    time_t rawtime;




while(true)
{

    hello();
    Time(rawtime);
    Sleep(100);


}




}

void hello()
{
    ofstream myfile;
    myfile.open("Hello.txt", ios::app);



    myfile << "hello";

}


int Time(time_t rawtime)
{


    time(&rawtime);



    ofstream myfile;
    myfile.open("Hello.txt", ios::app);



    myfile << ctime (&rawtime) << "\n";



    myfile.close();

    Sleep(6000);





}
Last edited on
Let's say I want to increment a number every 0.1 seconds and increment a second number only once every minute.
1
2
3
4
5
6
7
8
9
10
11
12
13
 
int num = 0, num2 = 0, counter = 0;
while(num2 < 10)
{
    ++num; 
    ++counter;
    if(counter == 600) //600 * 0.1 = 60 seconds
   {
        ++num2; 
        counter = 0; //Reset our counter
    }
    sleep(100); //Each tick of the loop is 0.1 seconds
}
Last edited on
Thanks that really helped a lot.Although it want the exact code i used it really helped me fix my problem.Thank you.
Topic archived. No new replies allowed.