How to correctly read and increment dates in c++ using localtime and mktime?

[NOTE]

You explicitly mention "using localtime and mktime" in the question's title, but I wasn't sure though after reading the rest of the text if that was mandatory, or you just needed to get a task done.

If you cannot use other libraries, just let me know and I'll remove this answer.


You could use std::chrono and Howard Hinnant's date library (C++11 onwards, header-only).
Or, should you be able to use a C++20 compiler, you would only need std::chrono.

[Demo]

#include <chrono>
#include <iostream>  // cout
#include <sstream>  // istringstream
#include <string>

#include "date/date.h"

int main()
{
    namespace ch = std::chrono;
    namespace dt = date;

    const std::string start_date{"2022-01-31"};  // date
    std::istringstream iss{ start_date };  // to string stream
    dt::sys_days start_day{};  // to a time point with a day duration
    dt::from_stream(iss, "%Y-%m-%d", start_day);
    
    for (auto day{start_day}, end_day{start_day + dt::days{3}};
         day < end_day;
         day += dt::days{1})  // with which we can do date arithmetic
    {
        std::cout << dt::format("%Y-%m-%d\n", day);
    }
}

// Outputs:
//
//    2022-01-31
//    2022-02-01
//    2022-02-02