Thread: Delay functioning weird...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    Delay functioning weird...

    I wanted the code below to put a $ on the screen, then wait like half a sec, followed by another, over and over. I tried different values as the value for a, and of course, they made it take longer or less time. However, for some odd reason, it seems to do the delay, then print the $'s so quick that one second there is 1 of them, next thing you know, you got all 49.

    As an aside, see that function to clear the screen at the top, I wrote that - I'm so proud of myself, lol.



    Code:
    #include <stdio.h>
    
    void cls() {
         printf("\033[2J");
         return;
    }
    
    
    int main() {
         cls();
    	long a;
    
    	int times;
    
    		for (times = 0; times < 50; times++) {
    
    	for (a = 0; a <10000000; a++);
    		
    		printf("$");
    
    		}

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    #include <windows.h>
    ...
    Sleep(1000); // sleep for 1 second


    This is far from portable, but its ok as a little exercise, I guess.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    great thanks i'm sure it will suffice for the time being

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > cls();
    > long a;
    Standard C doesn't have mixed declarations and statements.

    > printf("$");
    Unless you also flush the output stream, you may or may not see each indivdual character between delay calls.

    Do this
    printf("$");
    fflush( stdout );

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, 02:59 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07: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, 11:23 AM
  4. delay code
    By Grayson_Peddie in forum C# Programming
    Replies: 2
    Last Post: 07-15-2003, 11:02 AM
  5. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:28 AM