Thread: Extra Sleeping Info...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    Extra Sleeping Info...

    When using the way of sleeping that uses permanent loops, where does the number of seconds to sleep for go?

    Also, when I try to compile (although it may be because I don't know where to enter the seconds) my compiler tells me that:

    FILE NAME - CONFIDENTIAL: 56: `now'
    undeclared (first use this function)

    FILE NAME - CONFIDENTIAL: 56: `
    seconds' undeclared (first use this function)

    FILE NAME - CONFIDENTIAL: At global
    scope:

    FILE NAME - CONFIDENTIAL: 61: parse
    error before `else'

  2. #2
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    Post the code.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post some code
    Don't forget to read the posting guidelines at the top of the forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    2

    The Code

    #include <time.h>

    void sleep_seconds ( long seconds ) {
    clock_t limit, now = clock();
    limit = now + seconds * CLOCKS_PER_SEC;
    while ( limit > now )
    now = clock();
    }


    this is the code that i am having trouble with...

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    Yours compiles correctly on my machine. Post the entire code, it looks like there might by a global variable name conflict.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So much for mentioning the posting code notice....

    Your code compiles fine here with gcc, which compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question Do you have a main() function?

    ...I don't know where to enter the seconds...
    Do you have a main() function? All C++ programs need main().

    Then, to call your sleep_seconds() function for 100 seconds, you would put this in main():

    sleep_seconds(100);

    Or,
    sleep_seconds(x);

    Actually, you can call your sleep_seconds() function from any other function. You don't have to call it from main().... But, let's assume that your program isn't that complicated.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just a general question: is it better to use CLOCKS_PER_SEC or to just know what you're using and use your own constant?

    which is better:
    Code:
     limit = now + seconds * CLOCKS_PER_SEC;
    or
    Code:
     limit = now + seconds * 1000;
    I use the latter in most of my coding...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I use the latter in most of my coding...
    And yet the former is better...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >>And yet the former is better...

    that's what my question is... but that still leaves the obvious: why is it better?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    It's just better paractice.

    It's more portable. On some systems, CLOCKS_PER_SECOND may not be 1000.

    So, it's just a good habit to make all your programs as standard and portable as possible.

    On most systems, the execuitable is going to be the same using 1000, or CLOCKS_PER_SECOND. So, the final program won't be "better". But the source code will be more standardized.

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by ale_jrb
    #include <time.h>

    void sleep_seconds ( long seconds ) {
    clock_t limit, now = clock();
    limit = now + seconds * CLOCKS_PER_SEC;
    while ( limit > now )
    now = clock();
    }


    this is the code that i am having trouble with...
    Replace <time.h> with <ctime> and add "using namespace std;" as the first line in your function.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help displaying info from structure
    By kisiellll in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 12:51 PM
  2. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  3. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  4. pls help me extra info
    By sally arnold in forum C Programming
    Replies: 2
    Last Post: 01-15-2002, 10:50 AM
  5. Replies: 3
    Last Post: 12-06-2001, 05:30 AM