If you are using Windows and Dev-C++ then the function is Sleep(milliseconds) ...
#include <cstdlib>
#include <iostream>
#include <windows.h> // needed for Sleep(millisec)
using namespace std;
int main(int argc, char *argv[])
{
cout << "start..." << endl;
Sleep(2000);
cout << "done..." << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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:
cin.clear();
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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!
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Hmm maybe you should carry out the processing in your snippet only when the vector is not empty.
It can be easily done using something like:
vector<int> v;
for( int i = 0; i < 5; i++ ) {
v.push_back(i);
}
while( !v.empty() ) {
cout << v.back() << endl;
v.pop_back();
}
Hope it helped, bye.
[edit]
Or maybe you can in a smart way check if the user decided to leave the vector empty and in that case you can just create a default floating point vector for illustrating your point. That way it wont make a difference if the user leaves out the inputting part since your point will be very well driven home with your default array and the teachings will reach the needy :mrgreen:
[/edit]
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>C++ obviously doesn't like to process a totally empty vector. What would you recommend?
Have a guess.
Edit someone beat me to it.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I went with the second option and inserted ...
if (dV.empty())
{
cout << "... cannot process an empty vector so I made one up:\n";
dV.push_back(99.9);
dV.push_back(7.1);
dV.push_back(39.4);
dV.push_back(72.0);
}
Thanks again! Here, I thought I had forgotten all my C++ in favor of the addictive Python.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
You are most welcome Mr. Moderator :)
Hope you enjoyed your stay at the C++ forums
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
The way I feel, with Python you drive the car, and with C++ you fine_tune the carburetor and adjust the gears. Well, sometimes it's fun to get your hands dirty!
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Yeah even i respect Python really a lot coz of the top class games made using it (search www.gamespot.com for Severance:Blade of Darkness and Freedom Force) and also since its used in game scripting (stackless python for threading support).
Well I better stop going off the track least some Mod busts me :)
(if you have anything to ask or say just PM me)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734