double Sleep(rand()%0.1);

This is a discussion on double Sleep(rand()%0.1); within the C++ Programming forums, part of the General Programming Boards category; Code: double Sleep(rand()%0.1); invalid operands `int' and `double' to binary `operator %' I get this error when I compile this ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    43

    double Sleep(rand()%0.1);

    Code:
    double Sleep(rand()%0.1);
    invalid operands `int' and `double' to binary `operator %'

    I get this error when I compile this so how do I use it corectly and also what does the % sign mean or y do i haft to use it ?
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    % is modulus.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What are you trying to do?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    im trying to use 0.1 in Sleep(rand()%1); witch that code does work.
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Modulus (the % sign) finds the remainder when the first number in the expression is divided by the second number. It only works on integers, for floating point numbers I think theres a function that does this called fmod(), but I'm not sure as I have never used this.

    Are you calling a sleep function to make your program pause for a random amount of time? Time is usually measured in millisecs so you might be able to do something like:
    Code:
    Sleep(rand()%5000);
    to pause your prog for a random amount of time up to 5 seconds.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    ya i have that code working and thank you vary much i can atleast do some googling and maby figure out what i need

    i need a % of a millisecs like 0.4 and thats not working.
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You would need to use fmod for this. Heres an example of how to use it:

    http://www.cprogramming.com/fod/fmod.html

    Hope that helps.

    [edit]I'm not sure if you can make a delay less than one millisec tho[/edit]

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Sleep works on a millisecond basis since it takes a DWORD. You can't sleep less than 1ms and even if you try to sleep 1ms, you're probably going to sleep longer anyway, so there's no point.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    ok thanks to all of you I understand this now : )
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    hay wout would you guys say the best looping procedure is I here that goto loop; is hard to use curectly
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Depends on the situation. You have for, while and do...while. Show some sample code and we could help more.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Jordan,

    Exactly what are you trying to do? Why do you want to make a short delay?

    I'm asking, because what you're trying to do is probably possible, but requires a bit more information to understand what you actually want to do.

    By the way, in Linux there's a function "usleep" which takes a microsecond count. But that doesn't mean that if you give it a 1000 count, that the process will sleep for exactly 1ms - it may well sleep for 10ms [even if there's no other process to run on the system - if there is, there's no saying when your process will wake up, it could be 50 or 100 ms later].

    Windows also have the same principles at the base - if a process goes to sleep, there's no guarantee about when it will wake up, other than it will be AT LEAST as much as you specified later - but it could be 1ms more or 100ms more than you specified.

    If you want proper guarantees about what executes when. you need a real-time OS.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    62
    What about a timer? Is that as unprecise as Sleep too?
    This parameter is reserved

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ktulu View Post
    What about a timer? Is that as unprecise as Sleep too?
    Yes, that's what Sleep does - set a timer and wait for it to expire.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    matsp: what about the HPET-chip? you can program it to callback a function after a specified amount of time. Same is true for the RTC. Both have defined resolution so it should work reliable.

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

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 03:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM

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