Pause the Program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 38
Reputation: LieAfterLie is an unknown quantity at this point 
Solved Threads: 2
LieAfterLie's Avatar
LieAfterLie LieAfterLie is offline Offline
Light Poster

Pause the Program

 
0
  #1
Oct 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Pause the Program

 
0
  #2
Oct 1st, 2006
OS and Compiler?
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Pause the Program

 
1
  #3
Oct 2nd, 2006
Originally Posted by LieAfterLie View 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.
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Pause the Program

 
0
  #4
Oct 2nd, 2006
If you are using Windows and Dev-C++ then the function is Sleep(milliseconds) ...
  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. }
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pause the Program

 
0
  #5
Oct 2nd, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Pause the Program

 
0
  #6
Oct 2nd, 2006
Originally Posted by ~s.o.s~ View Post
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pause the Program

 
0
  #7
Oct 2nd, 2006
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.

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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Pause the Program

 
0
  #8
Oct 2nd, 2006
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pause the Program

 
0
  #9
Oct 2nd, 2006
Originally Posted by vegaseat View Post
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).

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:

  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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Pause the Program

 
0
  #10
Oct 2nd, 2006
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!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC