hey guyz... i'm new here... i just want to know how i could end my program after a certain amount of time has passed by...

I'm doing a "text twist" program where after 2.30minutes and if the user still has not guessed the correct answer, the program will end... i'm stuck on the timer part because i can't end the program after a certain amount of time....

here's the code i have for the input:

do
{
    cout << "Please input your guess here: " ;
    cin>> l;
 }while(b.check_answer(l));

could i use Sleep() for this part? i just need an point to the right direction 'cause i'm stuck right now.. thanks!:D

Dani AI

Generated

For a 2:30 overall timeout the hard part is that std::cin blocks. The suggestions already posted fall into two camps: run a watcher thread (as recommended) or sample the clock between inputs (as suggested). Sampling only helps when the program returns from input checks; it does not break a blocking cin. Also note that clock() is CPU-time, not wall-clock time, so it will not advance while waiting for user input (that makes ’s approach unsuitable for this interactive timeout).

A straightforward, portable C++11+ pattern is to compute a deadline with std::chrono::steady_clock and launch an async read for each guess, waiting only for the remaining time:

#include <iostream>
#include <future>
#include <chrono>
#include <string>

int main() {
    using namespace std::chrono;
    auto deadline = steady_clock::now() + minutes(2) + seconds(30);

    while (steady_clock::now() < deadline) {
        auto remaining = deadline - steady_clock::now();
        std::cout << "Please input your guess here: " << std::flush;

        auto fut = std::async(std::launch::async, []() {
            std::string s; std::getline(std::cin, s); return s;
        });

        if (fut.wait_for(remaining) == std::future_status::ready) {
            std::string guess = fut.get();
            // process guess; break when correct
        } else {
            std::cout << "\nTime's up!\n";
            return 0;
        }
    }
    std::cout << "Time's up!\n";
    return 0;
}

Notes and cautions:

  • Use steady_clock for elapsed-time measurement (it won’t jump if the system clock changes).
  • If an async read times out the background task may still be blocked on getline; process exit will terminate it. For a fully clean shutdown (no detached/leftover readers) platform-specific nonblocking input is required (POSIX select()/termios, or Windows console APIs).
  • Sleep() (Windows) or clock() have the limitations described above; threading or nonblocking I/O are the practical solutions for an interactive timeout.

Recommended Answers

All 3 Replies

What you can do is:

1. set a global to 0

2. start a second thread that sleeps 1 second 150 times in a loop,
on every loop it increments the loop counter, checks for a kill variable,
and if the loop counter is 150 sets the global to 1, and dies. if the kill variable is
1 it exits immediately, this allows a clean shutdown.

3. Your main thread checks for the global, and if it is 1, it exits.

The other way of doing it is to set a variable to time() + 150,
and on every loop get a new value for time() until it is GTE the
original cutoff time. If your gaime spends most of its time waiting
this should not be a problem, and you don't have to learn how
to deal with threads.

@plum
Aslong as he doesn't mind his program being 100% accurate the latter is good idea.

A bit more information about the latter for you,

time() comes with the including ctime,
i'd suggest having a read about the ctime header and all of its functions

Chris

I'd suggest that you use the clock() function. Examply of use:

#include <ctime>
#include <iostream>

#define SECONDS number of seconds here..

using namespace std;

int main(void)
{
int guess;
cin>>guess;

if(clock()>SECONDS*CLOCKS_PER_SEC) return 0;

return 0;
}

Note: clock() returns the number of milliseconds passed since the program began, and CLOCKS_PER_SEC is a defined constant, and its value is 1000;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.