problems

This is a discussion on problems within the C++ Programming forums, part of the General Programming Boards category; i wnat to make a clock that doesn't depend on the computer time or anything like that what i mean ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    problems

    i wnat to make a clock that doesn't depend on the
    computer time or anything like that
    what i mean is that i want to make a "handmade" clock.
    i made a loop like this:


    for(int sec=0;sec<60;sec++
    {
    _sleep(3000);
    cout<<sec;
    }
    i know it's wrong way but i think my brain has stopped working...

    u know the right way to write the code?
    please help if u do.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    :: shakes head ::

    This again?
    What exactly are you trying to do? And why do you refuse to use the functions in time.h?
    I guess I fail to see how you're going to make a "clock" without relying on the system at some point to keep it consistent.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    cause my teacher said so!

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    By your smiley, I'm thinking you put ";)" at the end of the loop... That's not right... Plus, you're pausing for 3 seconds, and then outputting the time...

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    As taylorguitarman said, the timer functions are linked to the system clock........so if your teacher wants you to create a clock without the use of the system clock......good luck!

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    why is that impossible?
    if only someone can show me how to pause a sec
    betwen the numbers in the loop
    i'll show u how u can make a clock without system time

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <stdlib.h>
    
    int main(){
    for(int sec=0;sec<60;sec++)
    {
    _sleep(1000);
    cout<<sec+1<<endl;
    }
    return 0;
    }
    OK.....now without the system clock......

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    y doesn't it work when iostream.h is included like i did

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>y doesn't it work when iostream.h is included like i did

    It does...I just use <iostream> and the using statements out of habit.....substitue the first 3 lines for

    #include <iostream.h> if you wish


    ...I guess I should have used <cstdlib> too instead of <stdlib.h>....but I forgot
    Last edited by Fordy; 02-14-2002 at 07:53 AM.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    it doesnt work for me

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    like this
    #include <iostream.h>

    #include <stdlib.h>
    #include<iomanip.h>
    int main()
    {


    for(int sec=0;sec<60;sec++)
    {
    _sleep(1000);
    cout<<setw(2)<<sec+1<<"\b\b\b\b";
    }


    return 0;
    }

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    What compiler are you using?

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    msvc++

  14. #14
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    #include <windows.h>

    Then you can use Sleep(1000);

    Here's the prototype:
    VOID Sleep(DWORD dwMilliseconds)

    But that still relies on the system clock.
    Last edited by taylorguitarman; 02-14-2002 at 09:51 AM.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Using Sleep is not a good idea for a clock. Sleep(1000) means to windows:- put this program to sleep for at least one second. There is nothing in the rules that states sleep(1000) wont pause for 2 seconds because I assure you that if your system is busy running many apps that could quite possibly happen often and that means your clock will lose time. The only way to correct your clock would be to read the system time and adjust accordingly. Therefore I would tell your teacher that this task is impossible. You can make a clock easily enough but it will not keep correct time.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21