943,758 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17755
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 1st, 2006
0

Pause the Program

Expand Post »
How would you make the program pause for a certain amount of time? I can't just tell it to do some trivial task over and over for so many loops, because the compiler seems to know its trivial and cuts it out. There's gotta be a simpler way anyway.
Similar Threads
Reputation Points: 23
Solved Threads: 2
Light Poster
LieAfterLie is offline Offline
38 posts
since May 2006
Oct 1st, 2006
0

Re: Pause the Program

OS and Compiler?
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Oct 2nd, 2006
0

Re: Pause the Program

How would you make the program pause for a certain amount of time? I can't just tell it to do some trivial task over and over for so many loops, because the compiler seems to know its trivial and cuts it out. There's gotta be a simpler way anyway.
Try to use sleep or usleep. I think that they are not standard functions but U can try. Use these func without including extra headers. The compiler will tell you that they are implicit declared. If they exist the linker will resolve this.
First function takes amount of seconds so for example sleep(1) will pause your program for one second, and the usleep uses microsecond usleep(1000000) will pause the prog for one second.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 2nd, 2006
0

Re: Pause the Program

If you are using Windows and Dev-C++ then the function is Sleep(milliseconds) ...
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <windows.h> // needed for Sleep(millisec)
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. cout << "start..." << endl;
  10. Sleep(2000);
  11. cout << "done..." << endl;
  12.  
  13. system("PAUSE");
  14. return EXIT_SUCCESS;
  15. }
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 2nd, 2006
0

Re: Pause the Program

Better not call the "system" functions like system ("pause").

Your job can be very well done using the function cin.get() which does the same function without invoking the system procedures which incur heavy overheads.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 2nd, 2006
0

Re: Pause the Program

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
Better not call the "system" functions like system ("pause").

Your job can be very well done using the function cin.get() which does the same function without invoking the system procedures which incur heavy overheads.
If you ever used Dev-C++ you would know that much of this code comes up as a templet. My point was to show how to use the Sleep() function. Talking about heavy overhead, the iostream header creates about 95% of the overhead.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 2nd, 2006
0

Re: Pause the Program

My post was not meant to contradict or show you wrong, just to point to the OP not to use the system functions when the same thing can be done using the std library functions.

This will be useful when he wants to write normal programs not involving "windows" specific code and needs the pause functinality.

Quote ...
Talking about heavy overhead, the iostream header creates about 95% of the overhead.
which cant be avoided when you are all set to write some concrete code snippets.
Last edited by ~s.o.s~; Oct 2nd, 2006 at 1:35 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 2nd, 2006
0

Re: Pause the Program

I agree with you, cin.get() is the portable way to wait, but take a look at a small code snippet I wrote a long time ago:
http://www.daniweb.com/code/snippet105.html

I would love, if you could explain to me why cin.get() is such a flop in this fairly simple case.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 2nd, 2006
0

Re: Pause the Program

Click to Expand / Collapse  Quote originally posted by vegaseat ...
I agree with you, cin.get() is the portable way to wait, but take a look at a small code snippet I wrote a long time ago:
http://www.daniweb.com/code/snippet105.html

I would love, if you could explain to me why cin.get() is such a flop in this fairly simple case.
Just thought would let you know that your program crashes when i dont enter any floating point input when asked for before entering "q" and pressing the RETURN key. (maybe you wanted to have it that way, i have no way of knowing, just my 2 cents).

Quote ...
I would love, if you could explain to me why cin.get() is such a flop in this fairly simple case.
This is because of the stray '\n' left in the input stream which occurs when the user presses the return key to convey his choice.

Just append the stmts:

C++ Syntax (Toggle Plain Text)
  1. cin.clear();
  2. cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
at the end of program before the cin.get () and everything should work out to be fine without that nasty "system" call which kills the so much loved portability.

Maybe you would want to take a look here:
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html

Hope it helped, bye.
Last edited by ~s.o.s~; Oct 2nd, 2006 at 2:41 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 2nd, 2006
0

Re: Pause the Program

Great, thanks ~s.o.s~ I finally was able to get rid of the old eyesore system("pause"). I knew there were stray '\n' around, but even the old double cin.get() trick didn't work here. Strangely enough, Narue mentioned that very same solution in another context. Should have listened and connected the dots

The "Enter some floating point numbers (q to quit)\n" part is not a feature, sometimes I just give up on foolproving things. I guess I never just pressed q or enter. C++ obviously doesn't like to process a totally empty vector. What would you recommend?

Thanks for the help and your interest!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Got the C++, what now?
Next Thread in C++ Forum Timeline: Implementing C++ into applications





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC