Hi! Me again, I have a feeling I will be posting here frequently now, I hope it's not too cumbersome.
I am now learning to create functions and I made a simple timer function. I spent half the day actually getting the damn thing to work and the second half trying to figure out how to add a user input keyword that will stop the timer immediately if entered.
I tried using string first, like in tutorial, but it doesn't work. As in I can't figure out how to make the control structures recognise it as a word and respond to it and it alone (it always throws either various compiling errors, or what more frequent access violation errors).
Then I tried using int values (just type in 1 to stop the timer), but I just can't get it to interrupt the timer.
Then I searched windows development centre, found some function called "WaitForInputIdle", thought "HurrDurr that looks useful!"... I crashed VS...
So I just ask here. Maybe you can help?
How to make an user-entered defined keyword (e.g. stop) stop the timer in this code:
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
|
#include <iostream>
#include <windows.h>
#include <ctime>
int timer(int st) //st is the time that the user can set (hence st - set timer)
{
using namespace std;
int sec;
sec = 0;
cout << "Timer starts. Tick occurs every 5 seconds \n";
for (sec = 0; sec <= st; sec = sec + 5)
{
cout << "Tick. " << sec << " seconds have elapsed \n";
SleepEx(5000, true);
}
return sec;
}
int main ()
{
using namespace std;
int time;
cout << "Enter how many seconds do you want (increments of 5) \n";
cin >> time;
timer(time);
cout << "Timer has elapsed.";
//cout << "Enter new value";
//Need to turn into a loop that repeats the main function upon timeout. I will do it later.
return 0;
}
|
?
I cleaned up the code a little to remove some of the embarrassing stuff I have been trying in vain.
The increment is there because I still am not entirely sure how the "for"
thing incrementation actually works so I am trying different things with it.
And changing 1 to 5 in increment is "different thing", no?
Timer returns seconds because I wanted to use it later when I figure out how to implement whatever value the function returns.