Thread: Displaying dates bug

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    18

    Question Displaying dates bug

    The program is supposed to have the user enter a date in the format of day month year. Example 12 12 2012 then dispaly it as 12th december 2012.
    When I run the code it displays 12h december 2012 or 1t december 2012 if the day entered is 1

    Code:
    /*exercise 3.2 dates*/
    #include
    <stdio.h>
    int
     main(void)
    {
        
    int monthnumber = 0;
        
    int day = 0;
        
    int year = 0;
        
    char ending = 0;
        printf(
    "\nEnter the date as three integer values for the day,the month and the year: \n");
        scanf("%d %d %d", &day, &monthnumber, &year);
        
    /*configure ending*/
        
    if (day >=4 && day <= 30)
            ending = 'th';
        
    elseif (day == 1 || day == 21 || day == 31)
            ending = 'st';
        
    else
            ending = 'rd';
        
    /*print day*/
        printf("\n%d%c", day, ending);
        
    /*Print Month*/
        
    switch (monthnumber)
        {
        
    case 1:
            printf(" January");
            
    break;
        
    case 2:
            printf(" February");
            
    break;
        
    case 3:
            printf(" March");
            
    break;
        
    case 4:
            printf(" April");
            
    break;
        
    case 5:
            printf(" May");
            
    break;
        
    case 6:
            printf(" June");
            
    break;
        
    case 7:
            printf(" July");
            
    break;
        
    case 8:
            printf(" August");
            
    break;
        
    case 9:
            printf(" September");
            
    break;
        
    case 10:
            printf(" October");
            
    break;
        
    case 11:
            printf(" November");
            
    break;
        
    case 12:
            printf(" December");
            
    break;
        }
        
    /*print year*/
        printf(" %d", year);
    }
    
    Last edited by prafiate; 08-13-2012 at 10:11 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    In the following snippet:
    Code:
    ending = 'st';
    The single quotes are only used for a single character. Also a char can only hold a single character, no more.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    In the following snippet:
    Code:
    ending = 'st';
    The single quotes are only used for a single character. Also a char can only hold a single character, no more.

    Jim

    So I would really only declare a variable as char if i wanted to store a singler character? otherwise I should use a character string?

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    Also my book hasn't gotten to character strings yet. So I guess i'll have to find another way to configure the ending for the days

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    So I would really only declare a variable as char if i wanted to store a singler character?
    Yes.
    otherwise I should use a character string?
    Yes.
    Also my book hasn't gotten to character strings yet.
    Then how are you using:
    Code:
    printf(" November");
    In the above November is a character string.

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    Code:
    if
     (day > 3 && day < 21 || day < 23 && day < 31)
            printf("\n%dth", day);
        
    elseif (day == 1 || day == 21 || day == 31)
            printf("\n%dst", day);
        
    else
            printf("\n%drd", day);
    This is what I changed, but I still have a bug. No matter what day I enter it always prints with the "th" ending.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The most glaring thing about your code is that you completely ignore the "nd" suffix. Also, in this comparison you mean day > 23 not day < 23
    Code:
    ((day > 3 && day < 21) || (day < 23 && day < 31))
    Another point, the case statement for the months is overkill. You could do this instead:
    Code:
    const char *months[] = {"January", "February", "..."};
    printf("%s", months[monthnumber]);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This deals only with the char append for the day number.

    Replace your logic for this completely.


    1) Use int n=day number % 10, to assign the right most digit of the day number, to n.

    2) Use a small char arrray like this:
    Code:
    char append[4][4]={
    {"th"},
    {"st"},
    {"nd"},
    {"rd"},
    {"th"}
    };
    Now the logic becomes simple:

    Code:
    #include <stdio.h>
    
    int main(void) {
       int i,n;
       char append[5][4] = {
          {"th"},
          {"st"},
          {"nd"},
          {"rd"},
          {"th"},
       };
       /* i represents the day number, here */
       for(i=1;i<110;i++) {
          n = i % 10;           //get the value of the dates right most digit, assigned to n
          if(i > 10 && i < 14)
             n=0;
          else if(n>4)
             n=4;
          
          printf("%4d%-3s ",i,append[n]);
    
       }
    
       printf("\n\n");
       return 0;
    }
    Last edited by Adak; 08-13-2012 at 12:57 PM.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    I haven't learned arrays yet, so I dont really understand the correction. I also don't quite pick up on the logic with the modulus.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by prafiate View Post
    I haven't learned arrays yet, so I dont really understand the correction. I also don't quite pick up on the logic with the modulus.
    What is "January" then? It's a string, and strings are one or more char's, ending with an end of string char: '\0'.

    a char array is simply something that holds char values - of course!

    Modulus is just used here to get the right most digit: Say you had 512, and you had code like:

    Code:
    int n;
    
    n = 512 % 10;
    What would n equal? 2 of course. Because we use a base 10 number system. Think about it - divide 512 by 10 and what's left over? 2!

    Divide 123 by 10, and what's left over? 3, etc.
    So 123 % 10 equals 3.

    etc.

    It's hard to do much programming involving letters, without using char arrays. Not worth spending a lot of time trying to work with char's, without learning about (and using), char arrays.

    You could get by using two separate char's, instead of a string, but why? Move forward, and make that jump - because you won't be using single char's to work with strings in the future, anyway. Nobody does.
    Last edited by Adak; 08-13-2012 at 12:49 PM.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    Oh alright thanks aot. That helped me tons!

  12. #12
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by prafiate View Post
    Also my book hasn't gotten to character strings yet. So I guess i'll have to find another way to configure the ending for the days
    From the comment in your code, it seems these may be practice exercises? if so I'd suggest attempting to solve the problem with the knowledge you've been taught up till now - from the book. The exercises are there to make sure you know what you've covered. Trying to jump ahead and using code you haven't really learned yet could have unexpected and frustrating consequences.

    Saying that, one solution to your problem would be to use multiple if and print statements to print out the characters you want.

    I'm also interested to know what book you are learning from as this seems to be the second time main() doesn't return an int. You may have to burn it and get a standard compliant
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  13. #13
    Registered User Obscenity's Avatar
    Join Date
    Aug 2012
    Location
    Tasmania
    Posts
    6
    I say scrap all that hard work and just use this...

    system("ECHO %DATE%");



    This is a simple program...

    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
    system("ECHO %DATE%");
    system("PAUSE");
    return 0;
    }
    Last edited by Obscenity; 08-13-2012 at 07:16 PM. Reason: Example Added

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    I'm also interested to know what book you are learning from as this seems to be the second time main() doesn't return an int. You may have to burn it and get a standard compliant
    Actually you are not required to return any value from main().
    From the Last C11 draft standard:
    5.1.2.2.3 Program termination
    If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;11) reaching the } that terminates the
    main function returns a value of 0.
    However I also recommend returning an int from main(), if for no other reason than to maintain consistency as every other function that is defined as returning a value must return a value of the proper type.

    Jim

  15. #15
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Website FAQ suggests return int from main.

    Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.
    Obscenity, your solution looks like it will only work on certain platforms, most likely MS?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dates
    By Choppers in forum C Programming
    Replies: 1
    Last Post: 06-30-2009, 03:01 AM
  2. Dates and C++
    By mmiskand in forum C++ Programming
    Replies: 17
    Last Post: 10-17-2008, 02:04 PM
  3. Getting the difference between dates
    By Leftos in forum C Programming
    Replies: 7
    Last Post: 01-20-2008, 12:49 AM
  4. Using dates in Pro*C
    By clancyPC in forum C Programming
    Replies: 0
    Last Post: 08-17-2006, 06:37 AM
  5. Dates
    By Chiz in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2002, 11:41 AM