5 Replies - 32800 Views - Last Post: 20 February 2009 - 02:27 PM Rate Topic: -----

#1 programminglearner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 86
  • Joined: 18-December 05

I need a time delay

Post icon  Posted 17 May 2006 - 03:10 PM

I need a time delay thing, I want to wait 5 seconds after and action happens, then another happens. here's my code right now:
#include <iostream>
#include <windows.h>

using namespace std;

int main(void)
{
string str;
int i;

cout << "This is a new 'GUESS THE NUMBER'!" << endl << 
"Try it out, you will get a hint if you get it wrong" << endl << endl;

do {
cout << "your guess is : ";
cin >> i;
if (i==32)
  cout << "CORRECT!  The right number was 32" << endl;
else if (i<=9)
  cout << "WRONG! the number is higher than 9" << endl;
else if (i>=51)
  cout << "WRONG! the number is below 51" << endl;
else if ( (i<=20) && (i>=10) )
  cout << "WRONG! the number is a multiple of 8" << endl;
else if ( (i<=31) && (i>=21) )
  cout << "WRONG! the number is a multiple of 4" << endl;
else if ( (i<=40) && (i>=33) )
  cout << "WRONG! the number is a multiple of 2 - :P (I know, it's not too helpful)" << endl;
else if ( (i<=50) && (i>=41) )
  cout << "WRONG! try again" << endl;
else 
  cout << "try again!" << endl;

} while(i!=32);
  
  return 0;
}


And by the way, the system("PAUSE"); code doesn't word, the 'press any key to continue...' thing doesn't pop up, it just stops.

Is This A Good Question/Topic? 0
  • +

Replies To: I need a time delay

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: I need a time delay

Posted 17 May 2006 - 03:14 PM

luckily, google's top rated search for a c++ sleep timer is hosted right here on dream in code.

http://code.dreaminc...t/snippet38.htm
Was This Post Helpful? 0
  • +
  • -

#6 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: I need a time delay

Posted 17 May 2006 - 03:49 PM

under windows the most effiecient way to stop the application for a period of time is the Sleep(DWORD dwMilliseconds)

#include <windows.h>

int main()
{
Sleep(1000); //Pause for 1 second
return 0;
}


Was This Post Helpful? 1

#7 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: I need a time delay

Posted 17 May 2006 - 10:32 PM

perfect answer Mrafcho001 :)
If you do not want to use the windows.h include, you can use the global sleep available
Thread.Sleep(1000); // 1 second pause


oddly enough this is the same as for java.
Was This Post Helpful? 0
  • +
  • -

#8 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: I need a time delay

Posted 18 May 2006 - 01:03 AM

i think sleep command is also availble under dos.h .so is delay() which works in milliseconds.
Was This Post Helpful? 0
  • +
  • -

#9 unreal_origin  Icon User is offline

  • Wisdom Listens
  • member icon

Reputation: 4
  • View blog
  • Posts: 742
  • Joined: 21-April 05

Re: I need a time delay

Posted 20 February 2009 - 02:27 PM

View PostDark_Nexus, on 17 May, 2006 - 04:14 PM, said:

luckily, google's top rated search for a c++ sleep timer is hosted right here on dream in code.

http://code.dreaminc...t/snippet38.htm



What happened to this code snippet :(
anyone know what i can do to make a sleep timer that frees up the CPU while it is in its delayed state (in the 5-10 ms range)

Also i cannot use windows.h headerfile since i am in unix/ sometime in FreeBSD

This post has been edited by unreal_origin: 20 February 2009 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1