# C++ Question (Yet another!)

**URL:** <https://boards.straightdope.com/t/c-question-yet-another/199195>\
**Category:** Factual Questions\
**Created:** [September 2, 2003, 7:32pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195 "2003-09-02T19:32:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayray5884](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@rayray5884](https://boards.straightdope.com/u/rayray5884)\
**Post date:** [September 2, 2003, 7:32pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195/1 "2003-09-02T19:32:22Z")

</div>

What I need to do is pause the screen numerous times. An array will be displayed, paused for x number of seconds and then the next array will be displayed. I know I can use a system(“pause”) in between each but I don’t really want to make the user hit a key in between every array. Is there a way to set it up so that there is a pause x seconds long in between each array? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Nanoda](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@Nanoda](https://boards.straightdope.com/u/Nanoda)\
**Post date:** [September 2, 2003, 7:49pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195/2 "2003-09-02T19:49:00Z")

</div>

All my C books are upstairs… man sez there’s a sleep() function in _\<unistd.h\>_.

```auto

SYNOPSIS
       #include <unistd.h>

       unsigned int sleep(unsigned int seconds);

DESCRIPTION
       sleep() makes the current process sleep until seconds seconds have elapsed or a signal
       arrives which is not ignored.

```

---

<div class="post-metadata">

**Author:** ![Mort\_Furd](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Mort\_Furd](https://boards.straightdope.com/u/Mort_Furd)\
**Post date:** [September 2, 2003, 7:51pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195/3 "2003-09-02T19:51:06Z")

</div>

A quick scan of my C++ reference (no C++ programmer should be without one) says that the “sleep” function will do the trick:

Giving a sleep(5); in your program would hold things for milliseconds.

How’s the class coming?

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [September 2, 2003, 7:52pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195/4 "2003-09-02T19:52:51Z")

</div>

Try the function “sleep”. It’s defined in different header files, depending upon exactly which C++ compiler you’re using. If it’s POSIX compliant, it’s defined in unistd.h. The argument is the number of seconds to sleep, and the return value is zero if all the time has elapsed, or the number of seconds left if the sleep call was interrumpted.

---

<div class="post-metadata">

**Author:** ![Mort\_Furd](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Mort\_Furd](https://boards.straightdope.com/u/Mort_Furd)\
**Post date:** [September 2, 2003, 7:58pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195/5 "2003-09-02T19:58:24Z")

</div>

Hokay. MS Sleep() does milliseconds, and by Nanoda’s post, unix stuff does seconds - and I know from my ancient Borland C++ V1.0 manual that that one did it by seconds in the DOS library.

---

<div class="post-metadata">

**Author:** ![rayray5884](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@rayray5884](https://boards.straightdope.com/u/rayray5884)\
**Post date:** [September 2, 2003, 8:30pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195/6 "2003-09-02T20:30:48Z")

</div>

Thanks everyone! [MVS.Net](http://MVS.Net) seems to like the windows.h include over the other. Overall class is going well, learning new stuff everyday.

---

<div class="post-metadata">

**Author:** ![William\_Ashbless](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@William\_Ashbless](https://boards.straightdope.com/u/William_Ashbless)\
**Post date:** [September 2, 2003, 8:54pm UTC](https://boards.straightdope.com/t/c-question-yet-another/199195/7 "2003-09-02T20:54:42Z")

</div>

Just a minor design comment: The more operating system or compiler specific your solution, the less cross-platform its going to be.

Sadly, there’s no completely cross-platform sleep solution that doesn’t involve sitting in a loop and spinning (if this is livable, its trivial to implement with a loop, and the standard time() or clock() function).

Yet another frustration for cross-platform development. Anybody wanna help me drag C++ into the next decade with a set of libraries at least equivalent to Java’s? (Heh, programmer flamebait).
