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(<);
long timeBefore = lt.wMilliseconds;
while (true) {
Sleep(50);
GetLocalTime(<);
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(<);
printf("The time is: %02d:%02d:%02d.%02d\n", lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
GetLocalTime(<);
timeBefore = lt.wMilliseconds;
}
}```