Timer Issues confusing...

This programm isn't working for my debugger/compiler and I need some assistance figuring out what the problem is... and please dont tell me not to use system, I already know about it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=time(0);
    int d=c;
    cout << "Enter how long that you want this timer to run\n           ";
    cin >> a;
    system("cls");
    a+d=b;
        while(b!=d){
        cout << time(0) << endl;
        Sleep(1000);
        system("cls");
}}


I am trying to make a stopwatch from scratch for my PC.

also look at my other topic that i need help with at:
http://www.cplusplus.com/forum/general/140776/
Last edited on
line 16 - you can't have a+d as the left hand side of an assignment. Did you mean to have b = a+d?
I would also suggest using std::this_thread::sleep_for and std::chrono.
oh..... I didn't catch that. Thanks.
But it still doesn't run correctly.
The loop is not stopping.

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
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=time(0);
    int d=c;
    cout << "Enter how long that you want this timer to run\n           ";
    cin >> a;
    system("cls");
    b=a+d;
    
                //loop Never ends
        while(b!=d){
        cout << time(0) << endl;
        Sleep(1000);
        system("cls");
}
        cout << "Timer complete!";
}
Last edited on
b and d are set before the loop begins, but nothing in the loop changes the values of b or d so that they eventually become equal to stop the loop.
What are you expecting them to enter for a?


1
2
    int c=time(0);
    cout << "time is " << c << endl
time is 1408384316


Last edited on
what is supposed to happen is as the time of the creation of the computer running the program goes up that number is the time so if i take that number and add to it it should count up to the new number and stop.

time is 140834930
Last edited on
You have the system sleep in the while loop, but the variables of b and d never get changed. The while loop needs to re-evaluate the current time to see if the requested timer length has passed.

I'm not on a windows machine right now, so I commented out the windows sleep command. I've used some of the other features of <ctime> here. (See http://www.cplusplus.com/reference/ctime/ )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    time_t start, finish, user, current;
    struct tm * startInfo, * finishInfo;
    cout << "Enter how long that you want this timer to run\n           ";
    cin >> user;
    start = current = time(0); 
    startInfo = localtime(&start);
    cout << "start time is " << asctime(startInfo);
    finish = start + user;
    finishInfo = localtime(&finish);
    cout << "finish time is " << asctime(finishInfo);
    cout << "timer length is " << difftime(finish,start) << " seconds" << endl;
    system("cls");
    
    while(current < finish)
    {
        //cout << time(0) << endl;
        //Sleep(1000);
        current = time(0); //update current time
        system("cls");
    }
cout << "Timer complete!";



Also as NoXzema suggested, you could also look into the chrono header ...
http://www.cplusplus.com/reference/chrono/

...or multithreading
http://www.cplusplus.com/reference/thread/this_thread/
Last edited on
I'm trying to create a count down timer based off of a counter that counts up and I want to simplify it as much as possible. I am also trying to use the count up from when the computer was made. "time(0);" this only tells me how long it would take.
Last edited on
You can use the Sleep to count up, I took that out because as I mentioned I can't use the Windows functions on this computer. But your loop is never ending because you never check to see if you've reached the end of the timed period. You start at a certain time, and your finish time is going to be that start time plus however many seconds the user says they want the timer to run. You need to reevaluate the current time in the loop so it will end eventually.



Edit: time(0) gets the current time. I'm not sure what you mean by the time when the computer was made.
Last edited on
time(0) is counting up from the creation of that computer.
This is the definition of the time() function that I'm familiar with that returns the current time. It's usually the # of seconds since Jan 1, 1970.

http://www.cplusplus.com/reference/ctime/time/

Get the current calendar time as a value of type time_t.

The function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer.

The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types
Then why was my time different than yours?
I want this:
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
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <conio.h>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int d=0;
    cout << "Enter how long that you want this timer to run\n           ";
    cin >> a;
    system("cls");
    b=a+time(0);

                //loop does end but
        while(b!=time(0)){
        system("color 3c");
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n                         " << a << endl;
        Sleep(1000);
        system("cls");
        a--;
}
        system("color 1f");
        while(d!=1){cout << "TIMER COMPLETE!\n";
        Sleep(1000);
        system("cls");
        cout << "Hit the SpaceBar!";
        Sleep(500);
        system("cls");
        if(GetAsyncKeyState(VK_SPACE)){d=1;}
        }
}
Last edited on
They were done at different times and possibly different time zones. What is the current time on your computer now? Mine is 8:30pm eastern US time on Monday.
Ok u r correct, thanks for the help :)
Topic archived. No new replies allowed.