Hi all... Can somebody explain to me why this code only works when I uncomment a specific line. It's not a crazy precision timer or anything just messing around atm, but don't understand the behavior.

#include <iostream>
#include <string>
#include <time.h>      // clock_t, clock, CLOCKS_PER_SEC

unsigned int timer(){
    clock_t t;
    t = clock();

    unsigned int curtime = 0;

    while(CLOCKS_PER_SEC != 2000){
        return curtime += t;
    }
}



int main(){
    std::string hello = "Wake up Neo...";
    unsigned int pos = 0;
    unsigned int milli = 0;
    unsigned int hunthou = 0;
    unsigned int tenthou = 0;
    unsigned int thou = 0;
    unsigned int hun = 0;
    while(true){  // runs forever atm
        if(timer() > milli){
            milli += 1;
            if(milli == 10){
                milli = 0;
                hunthou += 1;
                if(hunthou == 10){
                    hunthou = 0;
                    tenthou += 1;
                    if(tenthou == 10){
                        tenthou = 0;
                        thou += 1;
                        if(thou == 10){
                            thou = 0;
                            hun += 1;
                            if(hun == 10){
                                hun = 0;
                            }
                        }
                    }
                }
            }
            //if i uncomment the line below it works?????
            //std::cout << hun << thou << tenthou << hunthou << milli << '\n';
        }
        if(hun == 3){
            std::cout << "yes";
            hun = 0;
            thou = 0;
            tenthou = 0;
            hunthou = 0;
            milli = 0;
        }
    }
    return 0;
}

line 11: What??? That like saying while( 1 != 2) You could just delete lines 11 and 13 without changing that function.

unsigned int timer(){
    clock_t t;
    t = clock();
    unsigned int curtime = 0;
    return curtime += t;
}

And the above reduces to this:

unsigned int timer(){
    return clock();
}

Ok thank you I changed that but, the if statement at 51 is still not working correctly. Just wondering what that's about.

You need to either use your compiler's debugger so that you can view the values of variables, or just put some print statements in the program to print out the value of variables. I can't tell you why if(hun == 3){ (line 51) doesn't work the way you think it should.

This should work, I'll leave it to you to play 'spot the difference'

#include <iostream>
#include <string>
#include <ctime>// clock_t, clock, CLOCKS_PER_SEC

unsigned int timer()
{
    return clock();
}


int main()
{
    std::string hello = "Wake up Neo...";
    unsigned int pos = 0;
    unsigned int milli = 0;
    unsigned int hunthou = 0;
    unsigned int tenthou = 0;
    unsigned int thouthou = 0;
    unsigned int hun = 0;

    while (true)
    {  // runs forever atm
        if(timer() > milli)
        {
            milli += 1;
            if(milli == 10)
            {
                milli = 0;
                tenthou += 1;
                if(tenthou == 10)
                {
                    tenthou = 0;
                    hunthou += 1;
                    if(hunthou == 10)
                    {
                        hunthou = 0;
                        thouthou += 1;
                        if(thouthou == 10)
                        {
                            thouthou = 0;
                            hun += 1;
                            if(hun == 10)
                            {
                                hun = 0;
                            }
                        }
                    }
                }
            }

            std::cout << hun << '|' << thouthou << '|' << hunthou << '|' << tenthou << '|' << milli << '\n';
        }

        if (hun == 3)
        {
            std::cout << "\nyes!\npress Enter to continue...";
            std::cin.get();

            milli = 0;
            tenthou = 0;
            hunthou = 0;
            thouthou = 0;
            hun = 0;
        }
    }

    return 0;
}

Edited 2 Years Ago by nullptr

1.#include <iostream>
2.#include <string>
3.#include <ctime>// clock_t, clock, CLOCKS_PER_SEC
4.
5.unsigned int timer()
6.{
7.    return clock();
8.}
9.
10.
11.int main()
12.{
13.    std::string hello = "Wake up Neo...";
14.    unsigned int pos = 0;
15.    unsigned int milli = 0;
16.    unsigned int hunthou = 0;
17.    unsigned int tenthou = 0;
18.    unsigned int thouthou = 0;
19.    unsigned int hun = 0;
20.
21.    while (true)
22.    {  // runs forever atm
23.        if(timer() > milli)
24.        {
25.            milli += 1;
26.            if(milli == 10)
27.            {
28.                milli = 0;
29.                tenthou += 1;
30.                if(tenthou == 10)
31.                {
32.                    tenthou = 0;
33.                    hunthou += 1;
34.                    if(hunthou == 10)
35.                    {
36.                        hunthou = 0;
37.                        thouthou += 1;
38.                        if(thouthou == 10)
39.                        {
40.                            thouthou = 0;
41.                            hun += 1;
42.                            if(hun == 10)
43.                            {
44.                                hun = 0;
45.                            }
46.                        }
47.                    }
48.                }
49.            }
50.         // you uncommented this which is what i said it works when this is uncommented
            // otherwise it still does not work right hun always returns 3 if not????
51.            std::cout << hun << '|' << thouthou << '|' << hunthou << '|' << tenthou << '|' << milli << '\n';
52.        }
53.
54.        if (hun == 3)
55.        {
56.            std::cout << "\nyes!\npress Enter to continue...";
57.            std::cin.get();
58.
59.            milli = 0;
60.            tenthou = 0;
61.            hunthou = 0;
62.            thouthou = 0;
63.            hun = 0;
64.        }
65.    }
66.
67.    return 0;
68.}

Still not understanding the timer only works right if there is an std::cout statement above the if condition????

Ancient Dragon that's kinda the problem if I put a print statement in then it runs correctly but without it it doesn't lol.

What I actually want the code to do is to have the clock run for an amount of time and then be able to call another process with that output. So every 3 seconds for instance it will call the function or whatever. Thanks for the help guys to by the way. :)

At present your code is basically:

if (hun == 3)
{
    // reset variables to zero
}

So, given the above then it can't possibly go beyond 3.
The problem isn't that the code only works when the cout statement is present. Printing to console output just slows the execution of the looping code.
The real problem is that the code you have will never fulfill the requirements for a timer. If you want to create a timer, then you need to rethink the implementation.

Oh ok so what would be a good timer to use or how could I change the implementation to fit the requirements I am looking for?

Here's quite a simplistic example of a non high resolution timer that you should easily be able to incorporate in a class, extend its capabilities etc

#include <iostream>
#include <functional>
#include <thread>
#include <chrono>
#include <ctime>

void timer(int msecs, int reps, std::function<void()> callback_func)
{
    for (int i = 0 ; i < reps; ++i)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
        std::thread thread_func(callback_func);
        thread_func.detach();
    }
}

void fire() 
{
    clock_t t = clock();
    std::cout << "fired: " << t << std::endl; 
}

int main()
{
    std::thread my_thread(timer, 3000, 5, fire); // trigger fire() every 3 seconds for 5 repetitions
    my_thread.join();
    std::cout << "\nWe're done!\npress Enter to exit...";

    std::cin.get();

    return 0;
}

Nice nice looks like exactly what I was looking for but I don't have the thread directive. I'm using VC 10 Express atm. I'm assuming it maybe just easier to switch IDE's, but I'm wondering how they would have done it before VC 11 to.

Before c++11 you would have to use an os-dependent function to create a thread, or use boost thread class. For exampe, in MS-Windows you could call the win32 api function CreateThread() (windows.h header file)

Install VC++ 2013 which supports std::thread. Or use lastest Code::Blocks with MinGW (GNU) compiler

This question has already been answered. Start a new discussion instead.