Word Clock - midnight hour help

This is a discussion on Word Clock - midnight hour help within the C Programming forums, part of the General Programming Boards category; Hi, I am writing a program in C that tells the time in words, in English. I have a working ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    4

    Word Clock - midnight hour help

    Hi, I am writing a program in C that tells the time in words, in English. I have a working code to an extent, however I have a problem; when the time features the hour of 0, I want the program to read 'midnight' and not 'twelve'. At the moment, it reads 'midnightone'.
    Here is my code at the moment:
    Code:
        #include <stdio.h>    #include <time.h>    //Allows program to deal with time functions
        #include <windows.h>    //Allows Sleep() function
        #include <stdlib.h>
    
    
        /*Main clock function*/
        int main() 
        {
            int tm_min;    //initialising tm_min (minutes) variable
            int newmin;    //new variable for minute change
    
    
            char wordhour[][8] = {{"midnight"},{"one"},{"two"},{"three"},{"four"},{"five"},
                            {"six"},{"seven"},{"eight"},{"nine"},{"ten"},{"eleven"},{"twelve"}};    //array for converting hours into words
    
    
            time_t epoch_time;
            struct tm *tm_p;    //time value is tm_p (EG: hour is 'tm_p->tm_hour')
            epoch_time = time( NULL );
            tm_p = localtime( &epoch_time );    //Get time from local (system) time of the device.
    
    
            for(;;) 
            {
                system("cls");    //clears the screen
                        
                /*obtaining time standard*/
                char *time_str; 
                size_t len;
    
    
                time_t clock = time(NULL);
                time_str = ctime(&clock);
                len = strlen(time_str);    //set variable 'len' equal to length of 'time_str' string
                time_str[--len] = '\0';    // get rid of newline
                newmin = tm_p->tm_min;    //set 'newmin' equal to the current minute
    
    
                if (tm_p->tm_hour >= 13 && tm_p->tm_hour <= 23)    //If hour is pm, take 12 hours away to convert to 12 hour time.
                {
                    tm_p->tm_hour = tm_p->tm_hour - 12;
                }
                
                /*switch statement for minutes (EG: 'five to', 'almost', 'just gone'*/
                switch(newmin)    //Switch statement involving minutes
                {            
                        case 1:
                        {
                            printf("It's just gone %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 2:
                        {
                            printf("It's just gone %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 3:
                        {
                            printf("It's almost five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 4:
                        {
                            printf("It's almost five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 5:
                        {
                            printf("It's five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 6:
                        {
                            printf("It's just gone five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 7:
                        {
                            printf("It's just gone five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 8:
                        {
                            printf("It's almost ten past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 9:
                        {
                            printf("It's almost ten past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 10:
                        {
                            printf("It's ten past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 11:
                        {
                            printf("It's just gone ten past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 12:
                        {
                            printf("It's just gone ten past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 13:
                        {
                            printf("It's almost a quarter past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 14:
                        {
                            printf("It's almost a quarter past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 15:
                        {
                            printf("It's a quarter past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 16:
                        {
                            printf("It's just gone a quarter past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 17:
                        {
                            printf("It's just gone a quarter past %s.\n", wordhour[tm_p->tm_hour ] );
                            break;
                        }
    
    
                        case 18:
                        {
                            printf("It's almost twenty past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 19:
                        {
                            printf("It's almost twenty past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 20:
                        {
                            printf("It's twenty past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 21:
                        {
                            printf("It's just gone twenty past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 22:
                        {
                            printf("It's just gone twenty past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 23:
                        {
                            printf("It's almost twenty five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 24:
                        {
                            printf("It's almost twenty five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 25:
                        {
                            printf("It's twenty five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 26:
                        {
                            printf("It's just gone twenty five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 27:
                        {
                            printf("It's just gone twenty five past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 28:
                        {
                            printf("It's almost half past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 29:
                        {
                            printf("It's almost half past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 30:
                        {
                            printf("It's half past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 31:
                        {
                            printf("It's just gone half past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 32:
                        {
                            printf("It's just gone half past %s.\n", wordhour[tm_p->tm_hour] );
                            break;
                        }
    
    
                        case 33:
                        {
                            printf("It's almost twenty five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 34:
                        {
                            printf("It's almost twenty five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 35:
                        {
                            printf("It's twenty five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 36:
                        {
                            printf("It's just gone twenty five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 37:
                        {
                            printf("It's just gone twenty five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 38:
                        {
                            printf("It's almost twenty to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 39:
                        {
                            printf("It's almost twenty to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 40:
                        {
                            printf("It's twenty to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 41:
                        {
                            printf("It's just gone twenty to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 42:
                        {
                            printf("It's just gone twenty to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 43:
                        {
                            printf("It's almost a quarter to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 44:
                        {
                            printf("It's almost a quarter to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 45:
                        {
                            printf("It's a quarter to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 46:
                        {
                            printf("It's just gone a quarter to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 47:
                        {
                            printf("It's just gone a quarter to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 48:
                        {
                            printf("It's almost ten to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 49:
                        {
                            printf("It's almost ten to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 50:
                        {
                            printf("It's ten to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 51:
                        {
                            printf("It's just gone ten to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 52:
                        {
                            printf("It's just gone ten to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 53:
                        {
                            printf("It's almost five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 54:
                        {
                            printf("It's almost five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 55:
                        {
                            printf("It's five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 56:
                        {
                            printf("It's just gone five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 57:
                        {
                            printf("It's just gone five to %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 58:
                        {
                            printf("It's almost %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 59:
                        {
                            printf("It's almost %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        case 0:
                        {
                            printf("It's exactly %s.\n", wordhour[tm_p->tm_hour+1] );
                            break;
                        }
    
    
                        default:
                        {
                            printf("%s", time_str);            
                            printf("It's %d minutes past %s.\n", tm_p->tm_min, wordhour[tm_p->tm_hour] );
                            break;
                        }    
                }    
                
                if (newmin != tm_p->tm_min)
                {
                    return main();
                }
                else
                {
                    Sleep(10000);    //sleep for 10 seconds (10,000 milliseconds)
                }
            }
    
    
            return 0;
        }
    I would appreciate any help with this, thanks..
    Last edited by Logic; 02-21-2013 at 05:04 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,303
    Code:
    char wordhour[][8] = {{"midnight"}...
    The NUL character is omitted from the end of the midnight string - This is bad!

    You need to change it to:
    Code:
    char wordhour[][9] = {{"midnight"}...
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Ah, of course! I should have thought of this.. I suppose I have become unfocused after working on this code for a few hours, thank you very much for this answer! I couldn't for the life of me realise what was happening... haha

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    EDIT: too slow!

    Code:
    char wordhour[][8] = {{"midnight"},{"one"}...
    You only allow 8 characters for each word. Midnight has 8 letters, but you need one more spot, for the null character that must terminate all strings in C. What is happening here is there is no space for the null, and "midnight" and "one" are adjacent, so when printf tries to print "midnight", it goes until it finds a null. The null for midnight isn't there, so it keep going, straight on into "one", and stops at the null after "one".

    Since you never change the contents of wordhour, you can have a 1-d array of char pointers. No need to worry about ensuring enough space for each word, it's all taken care of.
    Code:
    char *wordhour[] = { "midnight", "one", ...};

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,303
    Quote Originally Posted by Logic View Post
    Ah, of course! I should have thought of this.. I suppose I have become unfocused after working on this code for a few hours, thank you very much for this answer! I couldn't for the life of me realise what was happening... haha
    Fresh eyes always helps!
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help beginner C++ class (need by midnight!!!)
    By maestroanth in forum C++ Programming
    Replies: 0
    Last Post: 03-21-2011, 08:13 PM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 05:02 AM
  3. One Last Question....I swear (its due at midnight!)
    By LittleLotte in forum C Programming
    Replies: 6
    Last Post: 02-02-2005, 08:04 PM
  4. Roidian: Final Hour (72 hour gd compo results)
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-29-2004, 09:27 PM
  5. Around the Clock. Hour function ??
    By Gugge in forum C Programming
    Replies: 1
    Last Post: 08-12-2002, 04:56 AM

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