Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,081 people online right now. Registration is fast and FREE... Join Now!




Input and Timers

 
Reply to this topicStart new topic

Input and Timers

Rulkiewicz
post 7 Oct, 2008 - 07:33 AM
Post #1


New D.I.C Head

*
Joined: 23 Sep, 2008
Posts: 2

I have a simple C++ program that asks for the users name. I want to be able to have a timer running in the background, and after 10 seconds with no "cin" data, I want the program to display "Time's up"

I've been reading through the forums looking in to timers, but I haven't been able to figure it out.

Any advice?
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 7 Oct, 2008 - 09:38 PM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


as far as I know, the input from user kind of functions are blocking functions. You can't proceed until user gives input.

If you are having so much dependency over it then I would suggest you to use threads for it.
one will be your main thread and other as your user input thread. Now you will have a timer interrupt which will be started as and when you start input thread. whenever the timer elapses, interrupt occurs and you will kill the input thread inside it.

I hope this will help you. smile.gif
User is offlineProfile CardPM

Go to the top of the page

overlord_shimra
post 8 Oct, 2008 - 11:36 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 13



Thanked 1 times
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 12:48PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month