Thread: delay()?/sleep()?/need for countdown/

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    delay()?/sleep()?/need for countdown/

    Well I've searched with the search function but never could get to be able to use the delay() or sleep() in my program...I'd like to, when the countdown starts (in the while part), that each number appears only after 1 second, so 10 (1 sec) 9 (1sec) 8...etc...
    Can anyone help me? Here's the program:

    #include <iostream.h>
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>

    int main()
    {
    char yesorno;
    cout<<"Begin countdown? Type y for yes or n for no (typing n will close the program):";
    cin>>yesorno;
    if(yesorno=='y')
    {
    int x=10;
    while(x>-1)
    {
    cout<<x<<endl;
    x--;
    sleep(1);
    }
    cout<<"BOOM! You are stupid, and dead. Press any key to quit the program.";
    }
    else if(yesorno=='n')
    {
    exit(1);
    }
    else
    {
    cout<<"Command not understood. Press any key to quit the program.";
    }
    getch();
    return 0;
    }
    "Nobility is not a birtright, but is defined by someone's actions." Robin Hood

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    void main( )
    {
       cout << "Going to sleep..." << endl;
       Sleep( 10000 );                                            //note the capital S
       cout << "I am wake after a 10 second sleep";
    }
    Enjoy

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    thanks!

    thanks a lot man!
    I'll keep that example for future reference!
    "Nobility is not a birtright, but is defined by someone's actions." Robin Hood

Popular pages Recent additions subscribe to a feed