Thread: how to delay in C++???

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    103

    how to delay in C++???

    i am not able to use sleep function in C++ by windows.h
    so i need a substitute function which takes milliseconds as arguments!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > i am not able to use sleep function in C++ by windows.h
    Why not?

    It takes milliseconds as a parameter
    Sleep function (Windows)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    only one error and these warnings!!!
    the tournament.o:the tournament.cpp.text+0x5b): undefined reference to `sleep(int)'
    the tournament.o:the tournament.cpp.text+0xfd): undefined reference to `sleep(int)'
    the tournament.o:the tournament.cpp.text+0x17f): undefined reference to `sleep(int)'
    the tournament.o:the tournament.cpp.text+0x258): undefined reference to `sleep(int)'
    the tournament.o:the tournament.cpp.text+0x2a8): undefined reference to `sleep(int)'\
    (here every smiley face shows ": (" )
    error
    collect2.exe: error: ld returned 1 exit status

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    now i'll reply after 8-20 hrs later its 1 hr left to midnight here and i m feeling ZZzzZZzz.....

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to study the link Salem provided, and notice the difference in spelling and remember C/C++ is case sensitive.

    Jim

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For that matter, how are you able to call sleep(...) in the first place? It doesn't exist, so it should generate a compile error. You didn't add some prototype manually, did you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    ummm..... no nothing is working i included the windows.h file + looked for all spell mistake and after that still this function is undefined!!!(some of you might think that i am just lying but i am serious)

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then post your code that exhibits the problem so maybe we can see what you're doing wrong.

    How did you spell the function sleep() or Sleep()?

    And how did you spell windows.h?



    Jim

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    there are 12 files including a header file but i'll show the one with int main()
    Code:
    #include"Player.h"
    #include<iostream>
    #include<conio.h>
    #include<time.h>
    #include<stdlib.h>
    #include<string.h>
    #include<windows.h>
    using namespace std;
    void clear();
    void noem();
    void story(int);
    void quit(int&);
        Player p;
        Player::enemy e;
    
    
    
    void ab()
    {
        system("cls");
        cout<<"you had purchased this item before\n";
        getch();
    }//end of ab()
    
    void noem()
    {
        system("cls");
        cout<<"you have not enough money to buy this item...\n";
        sleep(2000);
        system("cls");
    }//end of noem()
    
    void Player :: bou()
    {
        system("cls");
        cout<<"you bought this weapon named : "<<p.wpn;
        cout<<"and having damage : "<<p.dam[0]<<"-"<<p.dam[1];
        sleep(2500);
        system("cls");
    }//end of bou()
    void Player :: boa()
    {
        system("cls");
        cout<<"you bought this armour named : "<<p.armname;
        cout<<"and having strength : "<<p.arm;
        sleep(2500);
        system("cls");
    }//end of boa()
    
    void quit(int &j)
    {
        system("cls");
        char a;
        cout<<"are you sure want to quit?(y/n)";
        a=getch();
        switch(a)
        {
            case 'y':
            case 'Y':{
                    system("cls");
                    cout<<"thanks for playing this game ";
                    for(int i=0;i<10;i++)
                    {
                        cout<<(char)1<<" "<<(char)2;
                        sleep(2800);
                        cout<<"\b\b\b"<<(char)2<<" "<<(char)1;
                        sleep(2800);
                        cout<<"\b\b\b";
                    }
                    j++;
                }
            case 'n':
            case 'N':break;
            default :cout<<"\nchoose one of the two (y/n)\n";
                    quit(j);
        }//end of switch(a) inside function quit()
        cout<<"\n";
        system("pause");
    }//end of function quit()
    
    int main()
    {
        
        
        int reset=0;
        for(int i=0;i!=1;)
        {
        system("cls");
        char a;
        cout<<"welcome to \'the tournament\'\n";
        cout<<"press...\n (1) to start\n (2) for tutorial\n (3) for options\n (4) to quit";
        a=getch();
        cout<<"\n";
        switch(a)
        {
            case '1':p.start(reset,p,e);break;
            case '2':/**/break;
            case '3':p.options(reset,p);break;
            case '4':quit(i);break;
        }
        }//end of for
        return 0;
    }//end of main()

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look closely at the following snippet:

    Code:
    void noem()
    {
        system("cls");
        cout<<"you have not enough money to buy this item...\n";
        sleep(2000);
    Is that how Sleep() is spelled in the Microsoft documentation you have been provided. C/C++ is case sensitive.

    Jim

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    oh kayy then tell me is it small 's' or capital one???

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is not and has never been "sleep". It always has and always will be "Sleep".
    And fix those global variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    oh kayyyy got it its capital one i'll take care now actually i thought that its rare that we declare a function with capital letters!!! thanks
    jim

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not rare. It's a matter of style.
    Windows API functions are typically written LikeThis.
    The C++ standard library is typically written like_this.
    I tend to use a mix of both. It's a matter of style, and neither style is that rare.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    thanks Elysia i'll take care for that too!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delay for DOS
    By Erde in forum C++ Programming
    Replies: 34
    Last Post: 07-10-2011, 08:09 AM
  2. help.....!!!!!how much delay i will get by using delay(1)
    By gunjansethi in forum C Programming
    Replies: 7
    Last Post: 03-23-2010, 03:08 AM
  3. how to delay
    By llinocoe in forum C Programming
    Replies: 1
    Last Post: 09-22-2008, 06:09 AM
  4. I don't see a delay() in c++!
    By Makoy in forum C++ Programming
    Replies: 16
    Last Post: 12-25-2004, 08:21 AM
  5. delay
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-20-2002, 08:30 AM