Delay by second...

This is a discussion on Delay by second... within the C++ Programming forums, part of the General Programming Boards category; how do I make one? I have one at school but i forget how and i am at home right ...

  1. #1
    Lazy_Yoshi
    Guest

    Delay by second...

    how do I make one? I have one at school but i forget how and i am at home right now...

  2. #2
    Lazy_Yoshi
    Guest
    BTW, this is Wraith_Master, but kinda in a lazy mood right now

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Delay the program by a second? Use Sleep(1000), in windows.h
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <time.h>
    
    void Wait ( int Seconds );
    
    void Wait ( int Seconds ) 
    {
        clock_t endtime = clock() + Seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
    }
    The world is waiting. I must leave you now.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    void sleep(time_t seconds)
    {
       time_t now = time(NULL);
       while ((seconds + now) < time(NULL)) {};
    }
    There's a hack for ya.

  6. #6
    Lazy_Yoshi
    Guest
    thanks...

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    question

    I am a newbie programmer and dont understand this code:

    void sleep(time_t seconds)
    {
    time_t now = time(NULL);
    while ((seconds + now) < time(NULL)) {};
    }

    where do i put the number of seconds i want it to wait for. If I chose 10 secs will it look like this?:

    void sleep(time_t 10)
    {
    time_t now = time(NULL);
    while ((10 + now) < time(NULL)) {};
    }

    and what do i put in for now? im lost.

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Leave the sleep function alone.

    When you call sleep, do it like so.

    printf("Hello\n");
    sleep(3);

    That's all

    Please don't grave dig.

    Moerators?
    The world is waiting. I must leave you now.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    ok i tried.. still not working

    i dont get this here is my code:

    Code:
    #include "stdafx.h"
    #include <windows.h>
    #include <conio.h>
    #include <stdio.h>
    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>
    char playername[9];
    int caller;
    int clrscr(void);
    int getch(void);
    void sleep(time_t seconds);
    
    int main(void)
    {
       cout << "Still in the making Copyright 2003 All rights reserved.";
       printf("Press a key to continue...");
       getch();
       caller = clrscr();
       cout << "Enter your name (8 max characters): ";
       cin >> playername;
       cout << "Welcome " << playername << endl;
       sleep(100000);
       return 0;
    }
    
    int clrscr(void)
    {
       COORD coordScreen = { 0, 0 };
       DWORD cCharsWritten;
       CONSOLE_SCREEN_BUFFER_INFO csbi;
       DWORD dwConSize;
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
       SetConsoleCursorPosition(hConsole, coordScreen);
       return idisplay;
    }
    
    void sleep(time_t seconds)
    {
       time_t now = time(NULL);
       while ((seconds + now) < time(NULL)) {};
    }
    I got both the clear screen code and sleep code from this site and the clear screen thing works but not the sleep.
    Can anyone tell me whats wrong
    Last edited by fusoya77; 01-08-2003 at 08:26 PM.

  10. #10
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Ok, remove your function for sleep.
    Remove the prototype for sleep and all of the sleep calls.

    Since you're using windows.h simply call Sleep(number here) except make sure the S is uppercase.

    If that won't give you a go, use the delay function I submitted. Sleep restricts you to Windows.
    The world is waiting. I must leave you now.

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    thanks

    thank you so much that helped alot and it works now. So much easier than all that other stuff
    thanks a bunch
    Last edited by fusoya77; 01-08-2003 at 08:25 PM.

  12. #12
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I'm glad to hear it.

    By the way, since you're new, lets start you off on the right foot. When posting code, use code tags.
    The world is waiting. I must leave you now.

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    One more question. I want a user to input a number from 1 to 5 so i put the code cin >> inum;
    but if they input an interger that is not a num from 1 to 5 or a char it wouldnt work like i wanted it to also it would keep going throughout the program. In basic i would just do:
    Code:
    lbl question
    input blah blah
    if blah = 1-5
    then
    dothis stuff
    else
    goto question
    endif
    How can i do the same in c++?

  14. #14
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    If it was C, I could tell you. I'm not all that great in C++. I'm rusty as heck.

    Anybody want to give this person some help?

    I'd say research C++ if statements.
    The world is waiting. I must leave you now.

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    lbl question
    input blah blah
    if blah = 1-5
    then
    dothis stuff
    else
    goto question
    endif

    Code:
    do
    {
       int input;
       cout << "Enter number between 1 and 5: ";
       while (cin >> input)
          cout << "\nError! Must enter an integer: "
    } while (input > 0 && input < 6);
    
    dothisstuff();

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding delay in the connection
    By byatin in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-19-2008, 03:59 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 08:26 PM
  3. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-05-2005, 12:23 PM
  4. 2-3 Second delay with alarm()
    By Longie in forum C Programming
    Replies: 11
    Last Post: 06-20-2004, 09:46 PM
  5. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 07:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21