Thread: clock program

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    clock program

    Hey guys im making a clock program which displays the current time as a clock however say it 12 00 oclock it is suppose to print mH on the 12 its not printing in the correct spot.
    Does anyone know why?


    Code:
    void displayClock2(int * optionStats)
    {
       /*declarations*/
       struct tm *ptr; 
       time_t lt;
       int i, j;
       int tm_hour;
       int hour; 
       int a;
       char str[80];
      
       char clock2[9][20] = { /* The array to hold the hard-coded clock */
            "        12         ",
            "   11        01    ",
            " 10            02  ",
            "                   ",
            "09      .       03 ",
            "                   ",
            " 08            04  ",
            "   07        05    ",
            "        06         "
          };
    
       /* Row and column numbers in clock array for hour positions. For
       * example, the "06" in the clock display is stored at position
       * [8, 8] in the clock matrix (see above) */
    
       const int hr[13] = {0, 1, 2, 4, 6, 7, 8, 7, 6, 4, 2, 1, 0};
       const int hc[13] = {0, 13, 15, 16, 15, 13, 8, 3, 1, 0, 1, 3, 8};	
        
       /* Hour and minute codes */
       const char hs = 'H', ms = 'm';
       
       lt = time(NULL);
       ptr = localtime(&lt);
       
       strftime(str, 100, "%a %b %d %X %Z %Y", ptr); 
       /*hr = ptr -> tm_hour;*/
    
       /* copies ptr time struct into str string then 
       formats string to day month day of month time
       then time zone then year*/
       
       printf("\n");
       printf(str);
       printf("\n");
      
       /* once the struct gets copied into our formatted string
       it can get printed out to the screen*/
    
       /* updating the system hour within the clock array*/
       /*
       strftime(hour, 100, "%I", ptr); 
       strftime(minutes, 100, "%M", ptr);
       hour = (int) strtod(*ptr, hour);
       printf(hour);
       printf(minutes);
       */
       
       hour = ptr->tm_hour %12;
       
       clock2[hr[hour]][hc[hour]+1] = hs;
       clock2[hr[hour]][hc[hour]] = hs;
    
       /* Round m to the nearest hour position */
       a = ptr->tm_min % 5;
       if( a >= 3)
       {
         a = ptr->tm_min / 5 + 1;
         clock2[hr[a]][hc[a]] = hs;
         clock2[hr[a]][hc[a]+ 1] = hs;
                      
       }
       else
       {
         a = ptr->tm_min / 5;
    
       }
       
       /*if the minutes are equal to a zero like 12 00 m should
         be placed onto 12*/
       if(ptr->tm_min == 0)
       {
          ptr->tm_min =12;
       }
       /*if minutes fall onto the 60 m should b placed on the 12*/
       if(ptr->tm_min ==60)
       {
          ptr->tm_min = 12;
       }
       /* displays the mh symbol when two hands coincide*/
       clock2[hr[a]][hc[a]] = ms;
       clock2[hr[a]][hc[a]+ 1] = hs;
    
    
       /*
       
       clock2[hr[hour]][hc[hour]] = hs;
       clock2[hr[hour]][hc[hour]+ 1] = hs;*/
       
       /* Display the clock */
        for (i = 0; i < 9; i++)
        {
            for (j = 0; j < 19; j++)
        	    printf("%c", clock2[i][j]);
            printf("\n");
        }
        printf("\n");
    
    
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I remember this program from before. When you start a new thread on the same program, link to the old thread so that people know what has already been said about it.
    What is supposed to be happening here?
    Code:
    /* displays the mh symbol when two hands coincide*/
       clock2[hr[a]][hc[a]] = ms;
       clock2[hr[a]][hc[a]+ 1] = hs;
    You already set clock2[][] previously, and the hour and minute aren't going to coincide every time. You should put it in an if of some sort.

    As for this:
    Code:
       printf(str);
       printf("\n");
    I know str doesn't contain percent signs, but that's still probably not a good idea. printf() is a little slower anyway since it has to look for &#37;s. I'd use
    Code:
    puts(str);
    which does the same thing.

    BTW you never use the parameter optionStats.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > hour = ptr->tm_hour &#37;12;
    This should be:
    Code:
       hour = ptr->tm_hour;

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >   if(ptr->tm_min == 0)
    >   {
    >      ptr->tm_min =12;
    >   }
    This should be:
    Code:
       if (a == 0)
       {
          a = 12;
       }
    Code:
    >   if(ptr->tm_min ==60)
    >   {
    >      ptr->tm_min = 12;
    >   }
    Same here:
    Code:
       if (a == 60)
       {
          a = 12;
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM