Time Counter Problem

closed account (DEhqDjzh)
Hi, I have a program to count second 6 times faster than in the real life but something goes wrong:
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

time_t start;
double second;
double minute;
double hour;


void mainloop
{
start = time(0);


// some menu functions


double seconds_since_start = difftime(time(0), start);
if (seconds_since_start == 1)
{
	for (int i = 0; i < 6; i++)
	{
		second++;
	}
	seconds_since_start = 0;
}

if (second == 60)
{
	minute++;
	second = 0;
}
if (minute == 60)
{
	hour++;
	minute = 0;
}
std::cout << "Time Passed" << endl;
std::cout << second << "Seconds" << endl
std::cout << minute << "Minutes" << endl
std::cout << hour << "hour" << endl;

}




The problem is The second stops at random number like 13, 17 or sometimes 1 min 12 sec.
When I debugged, I saw that seconds_since_start value is -9.2559631349317831e+61 after loop
What should I do?
Last edited on
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
#include <iostream>
#include <chrono>
#include <thread>
#include <iomanip>

int main()
{
    using namespace std::chrono;

    // https://en.cppreference.com/w/cpp/chrono/duration
    using one_sixth_of_a_second = duration< double, std::ratio<1,6> > ;

    std::cout << std::fixed << std::setprecision(9) ;

    const auto start = steady_clock::now() ;

    for( int i = 0 ; i < 30 ; ++i )
    {
        std::this_thread::sleep_for( microseconds(99'750) ) ;

        const auto now = steady_clock::now() ;
        const auto diff = now - start ;

        // https://en.cppreference.com/w/cpp/chrono/duration/duration_cast
        const auto real_secs = duration_cast<nanoseconds>(diff).count() / 1'000'000'000.0 ;
        const auto simulated_secs = duration_cast<one_sixth_of_a_second>(diff).count() ;

        std::cout << "     real: " << std::setw(12) << real_secs << " seconds\n"
                  << "simulated: " << std::setw(12) << simulated_secs << " seconds\n\n" ;
    }
}

http://coliru.stacked-crooked.com/a/2ae2580fa8af0c5c
I think there are many problems with your code memeguy.

Do you realize that your code is missing a couple semicolons and you have std::cout while you haveendl without the namespace???

void mainloop()? I really cannot imagine what good does redirecting the int main() function will give...

Why are you going in a loop to increment a value 6 times, when you could just type in += 6?

And have you thought about the fact that you need to add a minute for every 60 seconds of the clock, not just 1 single minute??? And even if you set it up for it (you need to do the loops starting with hours, then minutes, then seconds), it would still be waste of time considering that the standard library can do it very efficiently...

This is how it is supposed to look like, but I think the chrono style looks much better if you want display just 1 unit of time, like seconds, and it has better accuracy because it can display floats.

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 <ctime>
#include <thread>
#include <chrono>


int main(void)
{
    std::time_t result;
    
    result = std::time(NULL);
    
    std::this_thread::sleep_for (std::chrono::seconds(1));
    
    //multiplied by 6 for request.
    result = (std::time(NULL) - result)*6;
    
    char mbstr[100];
    //%T is meerly a convenience format of %H:%M:%S"
    if (std::strftime(mbstr, sizeof(mbstr), "%T", std::localtime(&result))) {
        std::cout << mbstr << '\n';
    }

    //I cannot get this to work with cpp.sh for the life of me, but works on cppreferences's compiler.
    //std::cout << std::put_time(std::localtime(&result), "%T") << '\n';

    return(0);
}


You can also use chrono's system_clock and call std::chrono::system_clock::to_time_t to use chrono and have formatting in the same time. And in c++20 there will be a std::chrono::format, which does what my example does.
Last edited on
Topic archived. No new replies allowed.