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.

OS and Compiler?

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 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;
}

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.

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.

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.

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.

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.

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!

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]

Member Avatar for iamthwee

>C++ obviously doesn't like to process a totally empty vector. What would you recommend?

Have a guess.

Edit someone beat me to it.

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.

You are most welcome Mr. Moderator :)
Hope you enjoyed your stay at the C++ forums

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!

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)

Thanks, that's exactly what I wanted, Sleep() works perfectly. Capital S, milliseconds, like vegaseat said. All it needs is iostream though, not windows.h or anything else.

commented: Always nice to see someone post how there issue was resolved. +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.