pause () fuction

Pages: 12
Before you jump on me about the numerous ways to pause a program at the end with cin.get (), or the system ("pause") method, I will tell you right now that I'm trying to pause at the end of a module, and system ("pause") is not the route I'm going to take because I'm smarter than that.

I'm trying to pause the program for a number of seconds, and then have it continue. Now, I remember reading somewhere about the pause () command, and have used it in another program that I wrote. When I try to put that command in a new program that I'm writing, I keep getting the error " 'pause': identifier not found". I've looked through countless forums trying to find where I originally found the command and how to use it. Have any of you used that, or know what library or other commands I need to use to make this work? When I put this in the IDE, it gives me the tip pause (seconds), or something similar to that. I can't seem to duplicate it at this time. Any ideas?
Now, I remember reading somewhere about the pause () command
Nope. That's not a standard function.
See the example: http://www.cplusplus.com/reference/clibrary/ctime/clock/
Yes, it does. Thank you. I think I have a bigger problem though. I'm using both the time.h and windows.h libraries, and still getting the same error with sleep.
Last edited on
Alright, I'm posting a solution. I don't know why I can't find the pause or why it works in one place but not the other, but I'm not going to dwell on it. I used this solution here
http://www.cplusplus.com/forum/unices/10491/#msg51958

The only difference was I was not able to make it work with the 'sec' in there. Once removed it did what I wanted it to do. Thank you for the help!
the Sleep() function needs to be capitalized, Sleep(), not sleep()... I had that problem and it took me a while to figure it out... hope that helps... Also, you can do a simple Sleep(numberInSeconds * 1000) to convert from seconds to miliseconds, or if the time is going to be a constant, then just use the result of that formula, i.e. 1000 for 1 second, etc, in the Sleep function.

PS: Sleep() is in windows.h and is not cross-platform, obviously...
Last edited on
Sorry to re-post here, but I found some new information that may be benificial. I still don't know why my other pause works in the program, but it just does. I did however find this at another forum:
http://ubuntuforums.org/archive/index.php/t-42361.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <ctime>
#include <iostream>

void pause(int dur);

int main()
{
std::cout<<"Pausing for 2 secund... ";
pause(2);
std::cout<<"DONE\n";

return 0;
}

void pause(int dur)
{
int temp = time(NULL) + dur;

while(temp > time(NULL));
}


I think this would be an effective and cross-platform way to pause a program without continuous loop. I could be wrong. Let me know what you think.


I think this would be an effective and cross-platform way to pause a program without continuous loop.
What?

while(temp > time(NULL));
That there is a continuous loop, and hogs your processor time.


The sleep functions I posted for you waaay back in post number three, had you simply cut-and-pasted it, would work perfectly for you and not lock your process in a mindless loop.

Sorry to be so brutal, but when you start saying "this is the best way to do it" when it is not, ...
I'm not saying that it's the best way to do it. I was just trying to get feedback on another possible way. There's a reason I'm posting in the beginner forums. The other thing is I know that some of the sleep, wait, and other functions are Windows or Linux specific.

You're right though. It's testing in a loop. The loops I was actually refering too were the ones that were similar to

1
2
3
do {
i++
}while (i<3000)
Ugh. I remember using something like that to slow down QuickBASIC programs running on very old computers. That may not even work in C/++. Some compilers are smart enough to realize that the value of i isn't getting used for anything and will optimize the loop away.
I've seen it on other forums for something. I'm not even going to try.
Ugh. I remember using something like that to slow down QuickBASIC programs running on very old computers.


Ahhh, the good old daze! You buy a new PC and your favorite game runs so fast it's unplayable.

... Good Times.
You can always write a loop that copies a 1000-element vector of 1000-element vectors of doubles to another 1000-element vector of 1000-element vectors of doubles a couple of times :D
Or you can just use time(0), which is MIPS-independent and has no memory cost. If you're gonna do it wrong, at least do it wrong right.
Last edited on
Try this...

1
2
3
4
5
6
7
8
9
10
11
#include <ctime>

const int milsecs = 1000;


inline void pause(unsigned secs = 1U) // at least it pauses for 1 second...
{
    clock_t wait = secs * milsecs + clock(); // don't forget this! I mean the " + clock() "
                                                                 // dont rely calling it from the while only 
    while ( wait > clock() ) continue;
} 


I'm not saying that it's the best way to do it. I was just trying to get feedback on another possible way. There's a reason I'm posting in the beginner forums.


@Randon Noob: I will take your quote for a digression!

If you were Stroustrupp or Kalev or Meyers you wouldn't be writing to this forum for help; Then why someone "want-to-know-it-all" play into you for some code when what you need is some guidance! What the use of the forum anyway?? I've seen things like: someone comes with a doubt and someone else says: go to this link! or read that book! maybe i read the book and already went to that link and maybe what I need is another's point of view not that someone tells me that he knows more C++ than I do, you get it?? I would be ashamed if i have 1000 post and that 70% of those are go to: http://www.cplusplus.com/reference... kind of thing!

Question left: What the use???




Oh, look. It's the same function for the third time. And still wrong.

someone comes with a doubt and someone else says: go to this link! or read that book! maybe i read the book and already went to that link and maybe what I need is another's point of view
Maybe.
However, 99.5% of the time, they haven't. The problem most people have is not that they don't or can't understand. It's that they don't know how to or won't research on their own. There are few questions left that haven't been answered in detail somewhere on the Internet.
@DemienBjarne

Thanks for that code, I'll give it a shot to see what happens. I personally don't have access to a C++ book, nor the room to store it here, or store it for transit when I go home. And I'll be looking at a minumum of 4 weeks before one can be shipped out here. This is really been my only resource. This and a few other forums. The tutorial here is awesome. Thanks for the help!
You see you that's where I wanned to get!

Oh, look. It's the same function for the third time. And still wrong.


When or how is it right????

The question "what the used?" u serverd so nice!!!!! cuz if u cant say it, for the reason u might think( we cant go on presumptions here, i mean help or not!), then why bother listen to u!!!
When or how is it right????
I'm just going to assume that this means "what's wrong about it?"
clock() isn't guaranteed to return a time value in milliseconds. You're supposed to use CLOCKS_PER_SEC to find out what it returns.

The question "what the used?" u serverd so nice!!!!! cuz if u cant say it, for the reason u might think( we cant go on presumptions here, i mean help or not!), then why bother listen to u!!!
Is this kind of grammar inheritable, infectious, or something more like cancer?
@Random Noob: dont take care, pal, u can PM me I'll try to be of help!
Pages: 12