I’m trying to make a basic timer, this code seems to print out the number of milliseconds correctly based on the value in duration
But can someone tell me why it prints the output 4 times for each ‘tick’regardless of what the value of duration is, and how to correct it so that it only outputs once on every ‘tick’ set in duration? I’m sure it must be something simple I’m overlooking but I’m still learning the basics of C++ and I can’t see the error.
I’m running it on the iOS “Mobile C” app, but I don’t imagine that that would be what’s causing the problem.
#include <chrono>
#include <iostream>
int main()
{
using namespace std::chrono;
auto start = high_resolution_clock::now();
int duration = 100;
int i = 0;
while (i <= 100)
{
auto now = high_resolution_clock::now();
auto millis = duration_cast<milliseconds>(now - start).count();
if (millis % duration == 0)
{
std::cout << "millis: " << millis << std::endl;
i++;
}
}
}