program that slows down the windows clock speed the clock runs faster than expected

Viewed 47

I am trying to make a program that can set the speed of the Windows clock to x% speed. However the clock is running faster than it should when I run the program. What is wrong with this code?

#include <iostream>
#include <windows.h>
#include <stdio.h>

int main() {
    int timeFactor;
    SYSTEMTIME lt;

    std::cout << "Enter a number 1-100: ";
    std::cin >> timeFactor;

    GetLocalTime(&lt);
    long timeBefore = lt.wMilliseconds;
    while (true) {
        Sleep(50);
        GetLocalTime(&lt);
        long timeDifference = lt.wMilliseconds - timeBefore;//-900

        if (timeDifference < 0)
        {
            long timeToSet =  ((timeFactor / 100.0f) * -timeDifference) + timeBefore;
            lt.wMilliseconds = timeToSet;
        }else
        {
            long timeToSet = ((timeFactor / 100.0f) * timeDifference) + timeBefore;
            lt.wMilliseconds = timeToSet;
        }
        SetLocalTime(&lt);

        printf("The time is: %02d:%02d:%02d.%02d\n", lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
        
        GetLocalTime(&lt);
        timeBefore = lt.wMilliseconds;
    }
}```
0 Answers
Related