I was having a similar issue with my program. I got mine to work. I'll show you what I got.
CODE
#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;
void timerLoop(void*);
void input(void*);
int p-1;
int main()
{
_beginthread( timerLoop, 0, (void*)1);
_beginthread( input, 0, (void*)1);
while (p>9 || p<0)
{
cout << "";
}
return 0;
}
void timerLoop(void*)
{
int i=0;
while(p==-1&&i<20)
{
Sleep(1000);
i++;
}
if(p==-1)
cout << "time's up!" << endl;
p=0;
}
void input(void*)
{
cout << "Input a valid number: ";
cin >> p;
while (p>9 || p<0)
{
while(cin.fail())
{
cin.clear();
getline(cin, oops);
}
cout << "That is not a valid number. Input a corret number: ";
cin >> p;
}
}
the sleep command is in milliseconds. 1000 is 1 sec. what this does is it splits off 2 separate paths of operation aside from main. main keeps checking the variable p. once p enters the range you want, it terminates. if the user inputs a value into p then the while loop in the timer breaks and exits. I have it loop through each second so the timer doesn't waste resources when the timer isn't needed any more