Hi, i'm new here and this is my first tread. I'm a computer student and trying to gain usefull that may be valuable latter on, so...
Is there anyway to make a counter in c++? I've been looking and i've found none yet(similar but no what i want).
What i mean by a counter/timer is, if X doens't happen until Y seconds then Z happens. For example:
...
cout<< "What's your name?;
cin << name;
//and something like
if users takes more than 30seconds to answer then
cout << "to late";
...

got any ideas?
i'm starting, i can make programs, nothing to fancy but nothing great, So if there's any thing you've come across that has revealed itself valuable like this thread i read "I need special stuff in my project like system("cls")" PLEASE tell me. It would help alot!
Anyway, thanks. Hope to hear from you

Recommended Answers

All 10 Replies

I hitted the link, copied and pasted the code it lead me to, ran it but it didn't work. gave me many errors. the first one being: " boost/timer.hpp: No such file or directory. "
What am i doing wrong?

hi duude and welcome to daniweb. because c++ standard libraries dont gives any
thread functions for timers, we need to know what's your operating system is.
if its windows i'd be more than glad to help you

" boost/timer.hpp: No such file or directory. "

You'll need to download and build the library from http://www.boost.org/. Some of the facets of the library can be used with just the headers (as they are self-contained) but you'd still have to make sure you had all the dependencies (the headers include other headers, etc.). I'm not sure what the case is with Timer.

See http://www.boost.org/doc/libs/1_43_0/more/getting_started/windows.html (or if you need the *nix version it's the next page in.

It may be painful to install and get working the first time

I found this to be true also, but if it's what you need then go for it.

hi duude and welcome to daniweb. because c++ standard libraries dont gives any
thread functions for timers, we need to know what's your operating system is.
if its windows i'd be more than glad to help you

yeah, windows(7) =)

Wait how is boost timer supposed to help? He wants to time the input from the user.

commented: Yup +4

Ah. I commented on the availability of the boost without verifying what the function did. Props to firstPerson and apologies to the OP. I thought maybe the boost class offered a timer_tick() handler like in .NET.

There are at least 3 ways that I found to do it.

This one involves the ancient and nonstandard conio.h functions
http://www.programmersheaven.com/mb/CandCPP/310380/310380/time-out-of-getch--command/

This one offers a link to a win32 program partway down the page and goes into some of the options with pdcurses(http://pdcurses.sourceforge.net/ which is a slightly more modern interpretation of curses/conio)
http://www.gamedev.net/community/forums/topic.asp?topic_id=538714

@OP would you settle for something like this :

int main(){
 
 Timer timer; //Timer is some class that does timing stuff
 string input;

 timer.start();
 getline(cin,input);
 long milliSec= timer.getMilliSeconds();
 timer.reset();
 
 const int ALLOWED_TIME = 3000; //milli seconds
 if(milliSec > ALLOWED_TIME){
   //tell user time had passed
 }
 else{
   //input was in time
 }
}

I'm sorry, i'm pretty new to this, so if i'm doing something wrong because i don't understand i'm sorry.
I copy-pasted both the codes i found at:

http://www.programmersheaven.com/mb/...etch--command/ from jonsca

and the answer from firstPerson.

Since i guess i'm missing stuf they both gave multiple compiling errors.
While i don't understand some of the functions from the first link i can't say if it helps.
But the answer form firstPerson, if i can correct the compile errors, while usefull it's not exactly what i want(it helps but it's not exactly my goal).
So, and since I most probably didn't explain myself well enough i'll try to do it again:

let's supose i'm trying to do a math game. basicly it asks the user for answer for a calculation.Ex:
3 x 4 = ?
and while the users thinks there's a counter(displayed or not) and if the counter reaches, for example, 10 seconds whitout a answer being inputed it "aborts" the question and skips to something i want(display a message or something).

was i clear?:S
is it possible?

once again thank you and I'm sorry

For that code (at programmer's heaven) you'll have to include conio.h (if your compiler includes it, many will not,which comes with all the caveats of being non-standard) and include ctime to get the time function.

In that code they are taking a whole character array in, so you don't need most of it.

Try out the _kbhit in a couple of "toy" programs first, then add in the timing. See what you can come up with and post your code for this section and we can take a look.

...and no need to be sorry. This is just one of those areas where there's no clean way to do it is all.

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.