Thread: Time Pause

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Lightbulb Time Pause

    Hi I was wondering how I could have a pause in the program for a certain amount of time. Something like this:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <iostream>
    using namespace std;
    
    void wait ( int seconds )
    {
            clock_t endwait;
            endwait = clock () + seconds * CLK_TCK ;
            while (clock() < endwait) {}
    }
    
    void main()
    {
            text=getfile(filename);
            cout << "Please wait";
    	foutopen(filename);
    	for(int c=0;c<3;c++)
    	{	
    		cout << ". ";
    		wait(1);
    	}
    	letterfrequency(letfreq, text);
    	getwords(words, text);
    	sort(words,0,words.length()-1);
    
    	apvector <int> wordfreq(words.length(),-1);
    
    	wordfrequency(wordfreq, words);
    	output(letfreq, wordfreq, words, filename);
    }
    ignore the apvector and apstring stuff (it's a school program and they're gay and make me use it...
    I found this wait function at cplusplus.com and it worked by itself but when i put it into this code it doesn't work right. Any help would be greatly appreciated

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I can't find the problem, so try replacing it with this
    Code:
    void wait ( long seconds ) {
      clock_t limit, now = clock();
      limit = now + seconds * CLOCKS_PER_SEC;
      while ( limit > now )
        now = clock();
    }
    (taken from
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392 )

    EDIT
    If you can, using a nonstandard funtion is better to spare your poor cpu

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Thanks for the help, but i just figured out what was wrong. It kept the other stuff in the output buffer so...
    Code:
    cout << "Please wait";
    for(int c=0;c<3;c++)
    {	
            cout << ". ";
    	cout.flush();
    	wait(.5);
    }
    works. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 09:11 AM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM