Thread: Delay/Sleep help needed in C++

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    3

    Smile Delay/Sleep help needed in C++

    Greetings,

    I am very new to writing code and am attempting to alter a program I have by simply adding in a 40 second delay. After doing some searching online, it seemed like all I would need is to include the following header file text to the beginning of my program ( #include<stdio.h> and #include<stdlib.h> ) then I would be able to use the delay(40000); function. This does not seem to work. I am 100% certain I am doing something wrong and was wondering if anyone would able to offer some assistance.

    Please let me know if you need any further information. Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There is no standard library function for such a delay.

    Look into the OS-specific "Sleep()" and "sleep()" functions.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Great! So the "Sleep()" function is what I need. Thank you.

    I believe my implementation of the function is what I am unsuccessful with. I tried to include the sleep function but cannot successfully do so due to error(s). Is there a header file that is needed or is Sleep(40000); wrong? I am using MPLAB X by the way on Windows OS.

    My apologies by the way for asking any dumb/easy questions. I am very new to programming and this is my first time posting to a forum. Thanks in advance to any help!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you were writing a program in Windows, you'd need to include "Windows.h".

    If you're writing for a microcontroller, however, you'd need a delay function defined by your particular environment.

    MPLAB X is an IDE, so you need to determine what compiler you're using and look up delay in the documentation. This IDE typically comes bundled with the MPLAB XC compiler, which implements "_delay()", "__delay_ms()", and "__delay_us()" functions. See section 3.5.10 in the user's guide.

    Quote Originally Posted by Dlyons0129 View Post
    My apologies by the way for asking any dumb/easy questions. I am very new to programming and this is my first time posting to a forum. Thanks in advance to any help!
    No need for apologies, we were all beginners at some point. We are here to help.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Great! Yes, looks like it works. That user's guide spelled it out very well also. Thank you for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create a delay without using Sleep
    By Bag a Bones in forum C++ Programming
    Replies: 12
    Last Post: 01-25-2006, 10:35 PM
  2. delay or sleep?
    By GanglyLamb in forum C Programming
    Replies: 17
    Last Post: 12-12-2002, 11:46 PM
  3. sleep() or delay() headers?
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2002, 03:25 PM
  4. delay()?/sleep()?/need for countdown/
    By Robin Hood in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2002, 04:04 PM
  5. program delay (sleep)
    By rxg00u in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2002, 12:54 PM

Tags for this Thread