Grupo
Programación en Tiempo Real en C++
Ojetivo
Escriba un programa en C++ que muestre continuamente la hora actual y se actualice cada segundo. Utilice la biblioteca de tiempo para obtener la hora actual del sistema y formatearla para que muestre horas, minutos y segundos. El programa debe actualizar el reloj en tiempo real sin intervención del usuario y mostrar la hora actualizada cada segundo.
Desarrolle un sistema de reloj en tiempo real que se actualice cada segundo.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the input-output stream library
#include <chrono> // Include the chrono library for time-related functions
#include <thread> // Include the thread library to implement delays
using namespace std;
// Function to display the current time in hours, minutes, and seconds
void displayTime() {
while (true) { // Infinite loop to continuously update the time
// Get the current system time
auto currentTime = chrono::system_clock::now();
// Convert current time to a time_t type
time_t timeNow = chrono::system_clock::to_time_t(currentTime);
// Format the time to display it in a readable format
cout << "\r" << ctime(&timeNow) << flush;
// Wait for 1 second before updating the time again
this_thread::sleep_for(chrono::seconds(1));
}
}
int main() {
// Call the displayTime function to start the clock
displayTime();
return 0;
}
Salida
Sun Apr 16 12:34:56 2025
Sun Apr 16 12:34:57 2025
Sun Apr 16 12:34:58 2025
...
Comparte este ejercicio C++