Thread: clock 2

  1. #1
    Shadow12345
    Guest

    clock 2

    OK, why does this crash?
    #include <iostream>
    #include <time.h>
    #include <conio.h>
    #include <windows.h>

    using namespace std;

    int main()
    {
    time_t hold_t[500];
    for(long x=0; x <=500; x++)
    hold_t[x] = time(NULL);

    bool ACTIVE = true;

    while(ACTIVE)
    {
    cout << "\nThe time is " << hold_t << endl;
    system("CLS");
    if(x > 500)
    ACTIVE = false;
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    49
    >>for(long x=0; x <=500; x++)

    for(long x=0; x<500; x++)

    >>if(x > 500)

    if(x >= 500)
    Hello, everyone.

  3. #3
    Shadow12345
    Guest
    ok this program should appear as if it is updating the time
    #include <iostream>
    #include <time.h>
    #include <conio.h>
    #include <windows.h>

    using namespace std;

    int main()
    {
    bool ACTIVE = true;
    int x;
    time_t hold_t[500];
    for(x = 0; x < 500; x++)
    hold_t[x] = time(NULL);

    x = 0;
    while(ACTIVE)
    {
    cout << "\nThe time is " << hold_t[x] << endl;
    Sleep(500);
    system("CLS");
    x++;
    if(x > sizeof(hold_t))
    ACTIVE = false;
    }
    return 0;
    }

    for some reason it only shows the same time

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    49
    Yes, it should show the same time. Because your computer is so cool to do 500 times loop as a piece of cake.
    Hello, everyone.

  5. #5
    Shadow12345
    Guest
    ???

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    What he means is:

    Code:
    for(x = 0; x < 500; x++)
    hold_t[x] = time(NULL);
    each value of hold_t is given the current time, but since it take less than a second to go thru that entire loop, their values are all the same.

  7. #7
    Shadow12345
    Guest
    this doesn't work either
    #include <iostream>
    #include <time.h>
    #include <conio.h>
    #include <windows.h>

    using namespace std;

    int main()
    {
    bool ACTIVE = true;
    int x;
    time_t hold_t[500];
    for(x = 0; x < 500; x++)
    hold_t[x] = time(NULL);

    x = 0;
    while(ACTIVE)
    {
    cout << "\nThe time is " << hold_t[x] << endl;
    Sleep(1001);
    system("CLS");
    x++;
    if(x > sizeof(hold_t))
    ACTIVE = false;
    }
    return 0;
    }

  8. #8
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Well, after fiddling around with it I found the solution. I wish I would have known how to do this a few weeks ago.

    Code:
    #include <iostream> 
    #include <time.h> 
    #include <conio.c>   /*EDIT: NOTICE THAT I CHANGED conio.h to   
                                        conio.c */
    #include <windows.h> 
    
    using namespace std; 
    
    int main() 
    { 
    bool ACTIVE = true; 
    int x = 0; 
    time_t hold_t[500]; 
     
    while(ACTIVE) 
    { 
    hold_t[x] = time(NULL); 
    cout << "\nThe time is " << hold_t[x] << endl; 
    Sleep(1000); 
    system("CLS"); 
    x++; 
     if(x > sizeof(hold_t))
      { 
       ACTIVE = false; 
      }
    } 
    
    return 0; 
    }
    It increases the number by one every second.

    Also, in fiddling around with it few times I found a way to get a good random number generator. It skips all around the place.

  9. #9
    Shadow12345
    Guest
    Cool, that works exactly as I wished. I guess I needed to update every second, derrr....silly me.

  10. #10
    Shadow12345
    Guest
    just to let you know it only works with conio.h (for me, under visual studio)

    What compiler do you use?

  11. #11
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Cool, that works exactly as I wished. I guess I needed to update every second, derrr....silly me.
    Yeah, I know I was really glad to see it work too. A few weeks back I was trying to get a timer to work for my game, but I could never find anything that would work. Now that I know how to do a timer, I might go back and make a newer version of my game.

    What compiler do you use?
    For DevC++4.01 and below you have to use "conio.c". For all other compilers and versions of Dev I believe using "conio.h" works just fine.

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    #include <conio.c> /*EDIT: NOTICE THAT I CHANGED conio.h to
    conio.c */
    Okay, this is WRONG.
    you should NOT be including *.c files, instead you should add them to your project.

    If you do not know how to do this, look in the Dev-C++ Tutorial
    "There are three kinds of people in the world...
    Those that can count and those that can't."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  4. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  5. System clock
    By bazzano in forum C Programming
    Replies: 10
    Last Post: 03-27-2007, 10:37 AM