Grupo
Programación en Tiempo Real en C++
Ojetivo
Escriba un programa en C++ que tome un intervalo de tiempo en segundos del usuario y, tras cada intervalo, ejecute un evento (muestre un mensaje). El temporizador debe continuar ejecutándose hasta que el usuario lo detenga. Asegúrese de implementar un bucle que espere el intervalo de tiempo especificado antes de volver a ejecutar el evento.
Cree un temporizador de eventos con un intervalo de tiempo configurable.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the input-output stream library
#include <thread> // Include the thread library to implement delays
#include <chrono> // Include the chrono library for time-related functions
using namespace std;
// Function to implement the event timer
void eventTimer(int interval) {
while (true) { // Run the timer indefinitely (until the user stops it)
// Display the event message
cout << "Event triggered! Interval: " << interval << " seconds" << endl;
// Wait for the specified interval before triggering the next event
this_thread::sleep_for(chrono::seconds(interval));
}
}
int main() {
int interval;
cout << "Enter the time interval in seconds for the event: ";
cin >> interval; // Input the interval time in seconds
// Call the eventTimer function with the input interval
eventTimer(interval);
return 0;
}
Salida
Enter the time interval in seconds for the event: 3
Event triggered! Interval: 3 seconds
Event triggered! Interval: 3 seconds
Event triggered! Interval: 3 seconds
...
Comparte este ejercicio C++