Input while continually running

I am making a timer but I want to have an input while it is running. Here is my 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
using namespace std;

class timer
{
	private:
		unsigned long begTime;
	public:
		void start()
		{
			begTime = clock();
		}

		double elapsedTime()
		{
			return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
		}

		bool isTimeout(unsigned long seconds)
		{
			return seconds >= elapsedTime();
		}
};
void timermech(int seconds, int input, double timecounter);
int main(int nNumberofArgs, char* pszArgs[])
{
    int input = 1;
    int input2 = 1;
    double timecounter = 0;
    unsigned long seconds = 5;
    // 1 = nothing, 2 = open, 3 = close
    // 5 = setup, 1 = no setup
    for (;;)
    {
        cout << "Enter status: ";
        cin >> input;
        cout << endl << "Enter status 2: ";
        cin >> input2;
        cout << endl;
        if(input2 == 5)
        {
            cout << "Set timer: ";
            cin >> seconds;
            continue;
        }
        switch(input)
        {
            case 1:
            break;
            case 2:
            timermech(seconds, input, timecounter);
            break;
            case 3:
            break;
            default:
            continue;
        }
    }
}
void timermech(int seconds, int input, double timecounter)
{
    timer t;
    t.start();
    cout << "Timer started . . ." << endl;
    if(input != 1)
    {
        for(;;)
        {
            if(t.elapsedTime() >= seconds)
            {
                break;
            }
            timecounter = t.elapsedTime();
            while(t.elapsedTime() <= timecounter + 0.5)
            {
                cin >> input;
            }
            if(input == 3)
            {
                input = 1;
                break;
            }
        }
    }
    if(input != 3)
    {
        cout << t.elapsedTime() <<  " seconds elapsed" << endl;
    }
    cout << "Timer reset" << endl;
}


The bolded part is my attempt at trying to put in an input while the timer is running without having the other timer functions stop.
Bump?
Here is a method I have used before in the past. Some people might say that CLOCKS_PER_SEC is not accurate, but unless you are timing something down the nanosecond, I don't think this will affect your results greatly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double checkTime = 0;
    
    clock_t NewTime = clock();
    clock_t PrevTime = NewTime;
    
    while (1)
    {
        NewTime = clock();
        checkTime += (double)(NewTime - PrevTime);
        PrevTime = NewTime;
        
        if (checkTime > (double)(CLOCKS_PER_SEC))//Multiply CLOCK_PER_SEC by how often you want race to update
        {
            //Code you want to run in here
            
            checkTime -= (double)(CLOCKS_PER_SEC);
        }
    }
Thanks but how do I make an input while the clock is still running?

If I put cin<<... then it'll pause the clock and stuff
Line 14, you can do whatever you want in there

Because on line 16, it resets the clock again.
The only way I can think of doing it properly would be threading.

However, I feel that's a whole can of worms you don't want to open right now.
Sorry, I'm new, what's threading?
multi threading in the most basic terms is like having more than one main function running at once - parallel programming. Trust me, you don't wanna go there because the problem you are trying to solve is not that complicated as to start threading it.

mul·ti·thread·ing
/ˈməltiˈTHrediNG/
Noun
A technique by which a single set of code can be used by several processors at different stages of execution.

:( but I just want to know how to put input in.... Your solution, if I don't input anything, It'll keep going past time limit until I do...

Wait, where does your little snipet of program go anyway?
It goes no where, it is an infinite loop; you terminate it when you have achieved the result you were looking for.
I'm going to do some work...
Topic archived. No new replies allowed.