How to update code during execution?

I'm creating a choice-based survival game. I want it so that, upon entering the option screen, the options are displayed and a timer is triggered. If the user doesn't enter in a choice within, say, 10 seconds, they die.

The problem I've run into is trying to dual-execute the timer and cin statement. Ideally I want it to display "You died." (or something like it) after 10 seconds but before the user enters something. But they should be able to choose an option at any point within those 10 seconds.

I know I could just have it evaluate a timer after the user enters a value, so they have as long as they want to enter something, but if it takes longer than 10 seconds it returns dead after the value is entered.

I've tried using a built-in timer function, but I only was able to do the post-evaluation with it. I also tried threads, but not only did I get confused it also could only give me post-evaluation.

Edit: Didn't supply code, oops. Here it is, though not very helpful:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <ctime>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <windows.h>
#include <process.h>

  //Yeah, I know I have some unnecessary libraries. I just have them there in case I need them.

using namespace std;

int main()
{
    int choice = 0;


    cout << "Choose an option:\n";
    cout << "[1] - Option 1\n";
    cout << "[2] - Option 2\n";
    cout << "[3] - Option 3\n";
    cout << "[4] - Option 4\n";
    cout << "[5] - Option 5\n";
    cout << "[6] - Option 6\n";
    cout << "[7] - Option 7\n\n";

    if (choice == 1)
        cout << "test1";
    if (choice == 2)
        cout << "test2";
    if (choice == 3)
        cout << "test3";
                    // I just have some basic option returns here.
    return 0;
}



Like I said, I only have the gutted code for the options. I'd supply my attempts at timers and clocks, but they're honestly a joke because I didn't know what I was doing.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include <time.h>
#include <iostream>

using namespace std;

int main()
{
	time_t timer1, timer2;	
	double seconds;

	time(&timer1);  // get time

        // your function goes here
	int input;
	cin >> input;
		
	time(&timer2); // get time 
	
       // find the difference
	seconds = difftime(timer2, timer1);
        
	printf("%.f seconds since last timer update", seconds);     

	int x;
	cin >> x;

	return 0;
}
Last edited on
C++11 concurrency:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <future>
#include <chrono>
#include <atomic>

namespace
{
    std::atomic<bool> timeout ; // http://en.cppreference.com/w/cpp/atomic/atomic
    
    const int ERROR = -1 ; // error returned on timeout or eof
    
    const int MSECS_INTERVAL = 500 ; 
    const int MSECS_TIMEOUT = 10000 ;
}

int get_choice( int minv, int maxv )
{
    int v ;
    std::cout << "choose an option " << minv << '-' << maxv << ": " ;
    if( std::cin >> v && v >= minv && v <= maxv ) return v ;
    
    if( std::cin.eof() ) return ERROR ; 
    
    // http://en.cppreference.com/w/cpp/io/basic_ios/clear
    std::cin.clear() ; // clear failed state
    
    // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
    std::cin.ignore( 1000, '\n' ) ; // throw away incorrect input
    
    std::cout << "invalid input. please try again\n" ;
    return timeout ? ERROR : get_choice( minv, maxv ) ; // try again if not timed out
}

int main()
{
    timeout = false ;
    // http://en.cppreference.com/w/cpp/thread/async
    auto future = std::async( std::launch::async, get_choice, 1, 7 ) ;
    
    int millisecs = 0 ;
    while( millisecs < MSECS_TIMEOUT )
    {
           // http://en.cppreference.com/w/cpp/thread/future/wait_for     
           if( future.wait_for( std::chrono::milliseconds(MSECS_INTERVAL) ) == std::future_status::ready ) 
           {
               timeout = false ;
               break ;
           }
           else millisecs += MSECS_INTERVAL ; 
    }
    
    int choice = ERROR ;
    if( !timeout ) choice = future.get() ; 
    
    if( choice == ERROR ) 
    {
        std::cout << "input timed out. you can't read this because you are dead\n" ;
        // ...
    }
    
    else
    {
        std::cout << "your choice is: " << choice << '\n' ;
        // ...
    }
}

http://coliru.stacked-crooked.com/a/bf3aa2f06c3b84d5
Thanks for the replies. JLBorges, I tried running the code as you gave me, but I got 3 errors.

Here's the errors I got:
- " error: invalid use of incomplete type 'class std::future<int>' "
- " error: declaration of 'class std::future<int>' "
- " error: unable to deduce 'auto' from '<expression error>' "

So I'm pretty sure the 3rd error only happened because the first error happened, which only happened because of the second.
Both me and my C++ teacher were stumped on this, so I figured I'd come back here for some help.

It may be I'm not using the correct compiler, but I enabled CodeBlocks to use "-std=c++11".



rafae11, as for the other code, I just don't even have that library. I'm using CodeBlocks, and the stdafx.h library isn't working. From the look of it the equivalent library might be cstdio.h? What does
Decided to just remove it and test, and this is sort of the problem I was avoiding. The code counts up, and returns the time, but what I need is for it to return the clock while it's running an if statement, sort of. Once the timer hits 10, the timer doesn't wait for user input. It just immediately goes to the next block of code, in this case a terminating return so the program stops. So the code needs to evaluate the if statement constantly, which I'm not even sure is possible.
Check your MinGW version and if it is 32bit or 64 bit. There were problems with thread support Windows: http://stackoverflow.com/questions/10209871/c11-stdasync-doesnt-work-in-mingw

@JLBorges: Maybe you meant to initialize timeout to true?
Or it will always try to read choice from the future.
> Here's the errors I got:
> error: invalid use of incomplete type 'class std::future<int>

The MinGW build that you are using does not support C++11 concurrency.
( g++ -v would report, among other things, Thread model: win32. )

If you have the Microsoft compiler/library, switch to that; their library supports C++11 concurrency.
http://rextester.com/UPTD68474


> Maybe you meant to initialize timeout to true?

Yes. Thank you!
Last edited on
Topic archived. No new replies allowed.