'How can I change the values of the system clock using C++?

I've been trying to figure out how to use the system clock for a class project. The goal is to display the system's current time, then through inputs of 1-4 add one hour, minute, or second, then display the clock again. I think I have the basic framework down but I cannot figure out how to display the system time correctly as well as change the time itself. I have researched a few of the libraries to use and it gets pretty confusing with the pointers and the way it also always prints the date as well. I'm still new to C++ so my code is not the best especially when formatting the functions for the displays. Any help is appreciated.

When I run the program I want to print the local time from the PC it is running on, then receive input from the user (keystroke 1 - 4), then print the new time again. I don't want to actually change the time on my PC. For example say the current local time of my computer is 08:22:14, which will print to the screen. I wait any amount of time before I input selection 2. The new time will print 08:23:14.

#define _CRT_SECURE_NO_WARNINGS
#include <time.h>       /* time_t, struct tm, time, localtime */
#include <iostream> // std::cout, std::endl
#include <iomanip>  // std::setfill, std::setw
#include <stdlib.h> // system(CLS);
#include <Windows.h>
#include <ctime>
using namespace std;

int DisplayClocks(int time) {   // Function to write both clocks to screen  

    std::cout << std::setfill('*') << std::setw(26) << "     " << std::setfill('*') << std::setw(26) << " " << endl;    // First line of "*"
    std::cout << "*" << std::setfill(' ') << std::setw(16) << "12 Hour Clock" << std::setfill(' ') << std::setw(4) << "  *" << "     "
        << "*" << std::setfill(' ') << std::setw(18) << "24 Hour Clock" << std::setfill(' ') << std::setw(6) << "  *" << endl;
        // 12 hour clock
    std::cout << "*" << std::setfill(' ') << std::setw(6) << " " << time << std::setfill(' ') << std::setw(6) << "  *" << "     "
        // 24 hour clock
        << "*" << std::setfill(' ') << std::setw(8) << " " << std::setfill(' ') << std::setw(8) << "  *" << endl;
    std::cout << std::setfill('*') << std::setw(26) << "     " << std::setfill('*') << std::setw(26) << " " << endl;    // Last line of "*"
        
    return 0;

}

void DisplaySelection() {   // Function to display selection menu for user
    
    std::cout << std::setfill('*') << std::setw(26) << " " << endl;
    std::cout << "*" << std::setfill(' ') << std::setw(19) << "1 - Add One Hour" << std::setfill(' ') << std::setw(5) << "  *" << endl;
    std::cout << "*" << std::setfill(' ') << std::setw(20) << "2 - Add One Minute" << std::setfill(' ') << std::setw(4) << "  *" << endl;
    std::cout << "*" << std::setfill(' ') << std::setw(20) << "3 - Add One Second" << std::setfill(' ') << std::setw(4) << "  *" << endl;
    std::cout << "*" << std::setfill(' ') << std::setw(19) << "4 - Exit Program" << std::setfill(' ') << std::setw(5) << "  *" << endl;
    std::cout << std::setfill('*') << std::setw(26) << " " << endl;

}

void main()
{
    
    time_t now = time(0);
    system("CLS");
    
    string userVal;
    
    DisplayClocks(now); // Call displayClocks on program start
    DisplaySelection(); // Call DisplaySelection after display clocks
    cin >> userVal; // Take user input to modify clock display

    while (!( userVal == "Exit")) {
        // FIX ME: Add functionality to clear screen every second
        // FIX ME: Add displayClock to relevant if statements
        if (userVal == "1") {
            // Add One Hour to Clocks
            // FIX ME: Functionality for Displaying 12 and 24 hour clocks
            system("CLS"); // Clear screen test... working...
            DisplayClocks(now); // Call displayClocks on program start
            DisplaySelection(); // Call DisplaySelection after display clocks
            cout << "1" << endl;
            cin >> userVal;
        }

        else if (userVal == "2") {
            // Add One Minute to Clocks
            // FIX ME: Functionality for Displaying 12 and 24 hour clocks
            system("CLS"); // Clear screen test... working...
            DisplayClocks(now); // Call displayClocks on program start
            DisplaySelection(); // Call DisplaySelection after display clocks
            cout << "2" << endl;
            cin >> userVal;
        }

        else if (userVal == "3") {
            // Add One Second to Clocks
            // FIX ME: Functionality for Displaying 12 and 24 hour clocks
            system("CLS"); // Clear screen test... working...
            DisplayClocks(now); // Call displayClocks on program start
            DisplaySelection(); // Call DisplaySelection after display clocks
            cout << "3" << endl;
            cin >> userVal;
        }

        else if (userVal == "4") {
            // Exit Program
            // FIX ME: Functionality for Displaying 12 and 24 hour clocks
            cout << "Program Ended" << endl;
            break;
        }

        else{
            // Prompt user to input correct selection when not userVal ! (1-4)
            system("CLS"); // Clear screen test... working...
            DisplayClocks(now); 
            DisplaySelection();
            cout << "Error: Enter a selection 1 - 4." << endl;
            cin >> userVal;
        }   
    }
}


Solution 1:[1]

When you ask "How can I change SYSTEM <anything> in C++?", the keyword is "system".

It means that it's platform-dependent, so it needs a SYSTEM call - and I'm not speaking about the system function, but a call to your operating system's API, Win32 in your case. And a lot of these functions will requires elevation to work, on both Windows and Linux... Unfortunately for you, changing system date and time is such a function, on both OS, and it isn't allowed to call it with a standard account.

Also, it has nothing related with C++, in fact. On Windows, you'll need to call SetSystemTime, and it's not a C++ feature but a function of kernel32.dll imported through sysinfoapi.h header (found in Windows SDK). You'll get exactly the same answer in near any programming language: "Do a system call to SetSystemTime".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wisblade