Untitled
unknown
plain_text
2 years ago
991 B
10
Indexable
#include <iostream>
#include <chrono>
#include <thread>
int main() {
const std::chrono::seconds timeout(5); // Set a timeout of 5 seconds
auto start_time = std::chrono::steady_clock::now();
while (true) {
auto current_time = std::chrono::steady_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::seconds>(current_time - start_time);
if (elapsed_time >= timeout) {
std::cout << "Timeout reached. Exiting the loop." << std::endl;
break;
}
if (std::cin.peek() != EOF) {
char userInput;
std::cin >> userInput;
// Process userInput
start_time = std::chrono::steady_clock::now(); // Reset the timeout timer on input
}
else {
// No input available, continue the loop
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Optional: Sleep briefly to avoid busy-wait
}
}
return 0;
}
Editor is loading...