Warn the User

Jun 1, 2011 at 4:10am
Okay, with console programming, I want the user to hear an alarm every 5 seconds they do not answer a question. So, how do I do this?

1
2
3
4
5
6
7
8
9
10
cout<<"2 + 2";
cin>>answer;
if (answer != 4 && sleep (5000))
cout<<"\a Time is up!";
else if (answer !=4)
cout<<"Wrong answer";
else if (answer == 4)
cout<<"Correct Answer!";
else
cout<<"Numbers only, please."; 
Jun 1, 2011 at 5:58am
Impossible with the standard libs alone. In the standard lib, once you poll for input, it will wait indefinitely until the user responds. You can't tell it to stop waiting after X amount of time.

To do this, you'd either have to get into platform specific console I/O, use a 3rd party console I/O lib, or make the program multithreaded (ew).

Quite frankly it's not worth the effort.
Jun 1, 2011 at 11:34am
closed account (zb0S216C)
What Disch said. Personally, I would go for multi-threading. Threads are simple to create, and easy to understand. As for the alarm, including the <Windows.h> header will give you access to Beep( ).

Wazzak
Jun 1, 2011 at 2:15pm
I would go for multi-threading. Threads are simple to create, and easy to understand.


Ironically, I was thinking the exact opposite. Writing thread safe code is one of the trickier and harder things in programming.
Jun 1, 2011 at 2:42pm
Making a threaded application is not hard. As for making it thread safe...

You should give up the console and try your program with SFML. The console isn't great for this kind of thing IMO. If you use a multimedia library, you don't need multiple threads for the timing business, though getting user input might be more work, but not conceptually difficult.

(PS: As for the SFML input tutorial videos I promised you, I shall be uploading them eventually, I just ran into some delays in the mean time :p)
Last edited on Jun 1, 2011 at 2:42pm
Jun 1, 2011 at 11:54pm
Thanks for uploading (or future uploading) Xander :D
Topic archived. No new replies allowed.