Thread: Time Delay doesn't work!

  1. #1
    Unregistered
    Guest

    Unhappy Time Delay doesn't work!

    I'm writing a calender program, which cycles through the days outputting them to the screen. This is not going to be in real time.

    I've written the code. The problem I'm having is that the program should PAUSE for a certain amount of time (ie 3 secs) before moving onto the next day. However it doesn't, it outputs the days in BURSTS, rather than outputting one day, pausing and then outputting another day!

    Please help!?
    Code:
    #include <stdio.h>
    
    // Sets a multidimentional array for months
    char cmonth[ 12 ][ 10 ] = { "January" , "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    
    // Sets inital conditions for variables
    int z=0, i=0, c=0, p, days;
    int start=1, month, year=2001;
    
    // Sets inital constants for days 
    int cday[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
    
    // Declares functions
    void checkyear(), checkvalues(), pause();
    int caldays(), leapyear();
    
    
    int main(void)
    {
    
    printf("Enter Date to Start At (UK Format: dd/mm/yyyy):");
    scanf("%d/%d/%d", &start, &month, &year); 
    i = (month - 1); // Assign starting month
    checkyear(); 
    checkvalues();
    
    while(1) // Infinate Loop for Years
    {
            
            while (i < 12) // Loop for Months
            {
    
                    days = caldays(i); // Assign number of days for month
    
                      for (z = start; z <= days; z++) 
                      {
                            printf("\r%2.0d %9s %d  ", z, cmonth[ i ], year); 
                            pause();
                      }
            
                     i++ ; // Increment Month
                     start=1; // Reset to begining of month
                     
             }
             i = 0; // Resets months
             z = 0; // Resets days
             
             year++; // Increment year
    }
    
    }
    
    int caldays(i)
    {
            if (i == 1 && leapyear()) 
            {
                    return 29; // If so, returns days as 29
            }
            else
            {
                    return cday[i]; // Else, returns days for month i
            }
    }
    
    int leapyear(void)
    {
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) // Tests for leap year
            {
                   return 1; // If so, returns TRUE (1)
            }
            else
            {                
                   return 0; // Else, returns FALSE (0)
            }
    }
    
    void pause(void)
    {
               for (p=0; p=10000; p++)
               {
               ;
               }
    }
    
    void checkyear(void)
    {
            
            if (year < 1582)
            {
                    printf("\nThis program follows the Gregorian Calendar, it was first introduced in 1582. The year you have entered is less than 1582. Please enter a new date\n\n");
                    main();
            }       
            
    }
    
    void checkvalues(void)
    {
         if (month < 1 || month > 12 || start < 1 || start > cday[i])
         {
                 printf("\nDate entered is invalid. Please enter a new one\n\n");
                 main();
         }  
    }
    Thanks

    ZacPack

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Change this
    printf("\r%2.0d %9s %d ", z, cmonth[ i ], year);
    pause();


    To this
    printf("\r%2.0d %9s %d ", z, cmonth[ i ], year);
    fflush( stdout );
    pause();

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Talking

    Salem,

    Thanks! It worked!!!!
    Much appreciated!

    ZacPack

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting 2 while loops to work at the same time?
    By Tomkins in forum C Programming
    Replies: 3
    Last Post: 11-28-2008, 03:10 AM
  2. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 02:43 PM
  3. Printing a string with a time delay
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2004, 08:08 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM