King - Chapter 5 date.c

Discussion in 'C Programming' started by flebber, Dec 20, 2011.

  1. flebber

    flebber Guest

    I have recreated the program date.c from the book. However I am
    getting alot of errors when compiling the code. This is the code
    directly as I have typed it in.

    #include <stdio.h>

    int main(void)
    {
    int month, day, year;

    printf("Enter the date mm/dd/yyyy: ");
    scanf("%d / %d /%d" &month &day &year);

    printf("Dated this %d", day);
    switch (day) {
    case1: case21: case31:
    printf("st"); break;
    case2: case22:
    printf("nd"); break;
    case3: case23:
    printf("rd"); break;
    default: printf("th"); break;
    }
    printf(" day of ");

    switch (month){
    case1: printf("January"); break;
    case2: printf("February"); break;
    case3: printf("March"); break;
    case4: printf("April"); break;
    case5: printf("May"); break;
    case6: printf("June"); break;
    case7: printf("July"); break;
    case8: printf("August"); break;
    case9: printf("September"); break;
    case10: printf("October"); break;
    case11: printf("November"); break;
    case12: printf("December"); break;

    }
    printf(", 20%.2d.\n", year);
    return 0;

    }


    The errors start stating that we have duplicate cases, case1(day) is
    duplicate to case1 (month).

    Am I doing something wrong?
    These are the exact errors.

    || /home/sayth/My_Programs/C/dae.c: In function ‘main’:
    /home/sayth/My_Programs/C/dae.c|8 col 22 error| invalid operands to
    binary & (have ‘char *’ and ‘int’)
    /home/sayth/My_Programs/C/dae.c|23 col 3 error| duplicate label
    ‘case1’
    /home/sayth/My_Programs/C/dae.c|12 col 3| note: previous definition of
    ‘case1’ was here
    /home/sayth/My_Programs/C/dae.c|24 col 3 error| duplicate label
    ‘case2’
    /home/sayth/My_Programs/C/dae.c|14 col 3| note: previous definition of
    ‘case2’ was here
    /home/sayth/My_Programs/C/dae.c|25 col 3 error| duplicate label
    ‘case3’
    /home/sayth/My_Programs/C/dae.c|16 col 3| note: previous definition of
    ‘case3’ was here
    /home/sayth/My_Programs/C/dae.c|34 col 3 warning| label ‘case12’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|33 col 4 warning| label ‘case11’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|32 col 3 warning| label ‘case10’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|31 col 3 warning| label ‘case9’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|30 col 3 warning| label ‘case8’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|29 col 3 warning| label ‘case7’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|28 col 3 warning| label ‘case6’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|27 col 3 warning| label ‘case5’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|26 col 10 warning| label ‘case4’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|16 col 10 warning| label ‘case23’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|16 col 3 warning| label ‘case3’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|14 col 10 warning| label ‘case22’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|14 col 3 warning| label ‘case2’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|12 col 18 warning| label ‘case31’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|12 col 10 warning| label ‘case21’
    defined but not used [-Wunused-label]
    /home/sayth/My_Programs/C/dae.c|12 col 3 warning| label ‘case1’
    defined but not used [-Wunused-label]
    flebber, Dec 20, 2011
    #1
    1. Advertising

  2. flebber

    Eric Sosman Guest

    On 12/20/2011 5:49 PM, flebber wrote:
    > I have recreated the program date.c from the book. However I am
    > getting alot of errors when compiling the code. This is the code
    > directly as I have typed it in.
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > int month, day, year;
    >
    > printf("Enter the date mm/dd/yyyy: ");
    > scanf("%d / %d /%d"&month&day&year);


    Missing some commas here.

    > printf("Dated this %d", day);
    > switch (day) {
    > case1: case21: case31:


    Missing some spaces here and below. 'case1' and 'case 1' are not
    the same.

    > printf("st"); break;
    > case2: case22:
    > printf("nd"); break;
    > case3: case23:
    > printf("rd"); break;
    > default: printf("th"); break;
    > }
    > printf(" day of ");
    >
    > switch (month){
    > case1: printf("January"); break;
    > case2: printf("February"); break;
    > case3: printf("March"); break;
    > case4: printf("April"); break;
    > case5: printf("May"); break;
    > case6: printf("June"); break;
    > case7: printf("July"); break;
    > case8: printf("August"); break;
    > case9: printf("September"); break;
    > case10: printf("October"); break;
    > case11: printf("November"); break;
    > case12: printf("December"); break;
    >
    > }
    > printf(", 20%.2d.\n", year);
    > return 0;
    >
    > }


    In other news, I don't think much of the book's code, but perhaps
    he's bringing you along gently and not troubling you (yet) with harsh
    reality. A few points:

    - Whenever you take input from a user, you must be prepared for
    the input to be something ridiculous. For example, the user might
    interpret your instruction a bit too literally and type "mm/dd/yyyy"
    with no digits in evidence, or he might type "12/20//2011" by mistake.
    When something like this happens, scanf() will throw up its hands and
    report defeat -- and you should check for that possibility. scanf()
    returns a count of the numbers it was able to process; if that count
    isn't three you should realize something is wrong. (Further advice:
    Don't use scanf() for interactive input, because it's *much* harder
    than it appears.)

    - Even if you get three numbers from a successful scanf(), you
    should check that they're reasonable. In the code at hand nothing
    truly catastrophic will happen with crazy inputs, but you'll have to
    put up with "Dated this 33th day of , 20-99" if he types "33/0/-99".

    - The big `switch' to get the month names is butt-ugly. C has a
    construct called the "array" (perhaps the book hasn't covered it yet)
    that is much better suited to this kind of translation.

    --
    Eric Sosman
    d
    Eric Sosman, Dec 20, 2011
    #2
    1. Advertising

  3. On Thu, 22 Dec 2011 07:09:58 +0100, "io_x" <> wrote:

    snip

    >the problem is how to associate numbers to names
    >i find one soultion in assembly
    >write the months names
    >m0 db "January", 0
    >m1 db "February", 0
    >..
    >m11 db "December", 0
    >
    >than use some char pointer to point the month name
    >mm0 dd m0
    >mm1 dd m1
    >...
    >mm11 dd m11


    This won't work in any of the assemblers I use.

    >so each mont name number x is in mm0[x-1] or *(mm0+4*(x-1))
    >4 because 4 chars are 1 pointer size


    Not on the systems I use.

    >in C this could be something as
    >char *m0="January", *m1="February", ..., *m11="December";
    >char **mm[12];
    >and than one function for initialize mm[12] first of use
    >the library
    >something as
    >int dateInit()
    >{int i;
    > mm[0]=m0; mm[1]=m1; etc
    >}


    By your definition, mm[0] is a char** and m0 is a char* so this won't
    even compile. You have made a relatively simple process overly
    complicated. It would be much simpler to have
    char *mm[12] = {
    "January",
    "February",
    ...
    "December"};
    which creates what you apparently intended without all the superfluous
    variables.

    --
    Remove del for email
    Barry Schwarz, Dec 22, 2011
    #3
  4. flebber

    Jim Guest

    From the book's website, they have the code:

    /*********************************************************
    * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
    * By K. N. King *
    * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
    * All rights reserved. *
    * This program may be freely distributed for class use, *
    * provided that this copyright notice is retained. *
    *********************************************************/

    /* date.c (Chapter 5, page 89) */
    /* Prints a date in legal form */

    #include <stdio.h>

    int main(void)
    {
    int month, day, year;

    printf("Enter date (mm/dd/yy): ");
    scanf("%d /%d /%d", &month, &day, &year);

    printf("Dated this %d", day);
    switch (day) {
    case 1: case 21: case 31:
    printf("st"); break;
    case 2: case 22:
    printf("nd"); break;
    case 3: case 23:
    printf("rd"); break;
    default: printf("th"); break;
    }
    printf(" day of ");

    switch (month) {
    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;
    }

    printf(", 20%.2d.\n", year);
    return 0;
    }
    Jim, Dec 23, 2011
    #4
  5. pete <> writes:
    <snip>
    > I agree with Barry Schwarz:
    > "You have made a relatively simple process overly complicated."
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int
    > main(void)
    > {
    > int month, day, year, rc;
    > char *month_name[] = {
    > "January",
    > "February",
    > "March",
    > "April",
    > "May",
    > "June",
    > "July",
    > "August",
    > "September",
    > "October",
    > "November",
    > "December"
    > };
    >
    > printf("Enter date (mm/dd/yy): ");
    > rc = scanf("%d /%d /%d", &month, &day, &year);
    >
    > if (rc != 3 || 1 > month || month > 12) {
    > puts("input problem");
    > exit(EXIT_SUCCESS);
    > }
    >
    > printf("Dated this %d", day);
    > switch (day) {
    > case 1:
    > case 21:
    > case 31:
    > printf("st");
    > break;
    > case 2:
    > case 22:
    > printf("nd");
    > break;
    > case 3:
    > case 23:
    > printf("rd");
    > break;
    > default:
    > printf("th");
    > break;
    > }
    >
    > printf(" day of %s, 20%.2d.\n", month_name[month - 1], year);
    > return 0;
    > }


    In the same spirit as using an array for the month names, you can also
    do this:

    const char *ord[] = { "th", "st", "nd", "rd", "th",
    "th", "th", "th", "th", "th" };
    ...
    printf("Dated this %d%s day of %s, 20%.2d.\n",
    day, ord[day % 10], month_name[month - 1], year);

    Apart from being neater, it has the advantage of not breaking up the
    output with an intervening switch statement. You can keep the format
    string in one piece.

    --
    Ben.
    Ben Bacarisse, Dec 23, 2011
    #5
  6. flebber

    BartC Guest

    "Ben Bacarisse" <> wrote in message
    news:...
    > pete <> writes:


    >> printf("Dated this %d", day);
    >> switch (day) {
    >> case 1:
    >> case 21:
    >> case 31:
    >> printf("st");
    >> break;
    >> case 2:
    >> case 22:
    >> printf("nd");
    >> break;
    >> case 3:
    >> case 23:
    >> printf("rd");
    >> break;
    >> default:
    >> printf("th");
    >> break;


    > In the same spirit as using an array for the month names, you can also
    > do this:
    >
    > const char *ord[] = { "th", "st", "nd", "rd", "th",
    > "th", "th", "th", "th", "th" };
    > ...
    > printf("Dated this %d%s day of %s, 20%.2d.\n",
    > day, ord[day % 10], month_name[month - 1], year);
    >
    > Apart from being neater, it has the advantage of not breaking up the
    > output with an intervening switch statement. You can keep the format
    > string in one piece.


    I haven't tried this, but does it work for day 11 and day 12 too?

    Although if not, making those an exception, would still be less code than
    the switch.

    --
    Bartc
    BartC, Dec 23, 2011
    #6
  7. "BartC" <> writes:

    > "Ben Bacarisse" <> wrote in message
    > news:...

    <snip>
    >> const char *ord[] = { "th", "st", "nd", "rd", "th",
    >> "th", "th", "th", "th", "th" };
    >> ...
    >> printf("Dated this %d%s day of %s, 20%.2d.\n",
    >> day, ord[day % 10], month_name[month - 1], year);
    >>
    >> Apart from being neater, it has the advantage of not breaking up the
    >> output with an intervening switch statement. You can keep the format
    >> string in one piece.

    >
    > I haven't tried this, but does it work for day 11 and day 12 too?


    No, you are absolutely right, it does not work for those. The obvious
    fix would be 20 strings, but that's getting ugly. There is probably
    some more obscure expression that can cut down the number of strings
    needed but it will start to lose clarity.

    <snip>
    --
    Ben.
    Ben Bacarisse, Dec 23, 2011
    #7
  8. On Fri, 23 Dec 2011 12:09:44 -0500, Ben Bacarisse <>
    wrote:

    > "BartC" <> writes:
    >
    >> "Ben Bacarisse" <> wrote in message
    >> news:...

    > <snip>
    >>> const char *ord[] = { "th", "st", "nd", "rd", "th",
    >>> "th", "th", "th", "th", "th" };
    >>> ...
    >>> printf("Dated this %d%s day of %s, 20%.2d.\n",
    >>> day, ord[day % 10], month_name[month - 1], year);
    >>>
    >>> Apart from being neater, it has the advantage of not breaking up the
    >>> output with an intervening switch statement. You can keep the format
    >>> string in one piece.

    >>
    >> I haven't tried this, but does it work for day 11 and day 12 too?

    >
    > No, you are absolutely right, it does not work for those. The obvious
    > fix would be 20 strings, but that's getting ugly. There is probably
    > some more obscure expression that can cut down the number of strings
    > needed but it will start to lose clarity.


    If you really wanted to do it without the intervening switch statement,
    you could replace the use of the ord array with an ugly set of nested
    ternary expressions. Or hide the switch statement in a helper function,
    so that ord[day % 10] would become ord(day).

    While we're at it, if I were writing this with support only for 2-digit
    year input, with the assumption that the first two digits of the year
    are "20", I would probably add

    if (year < 0 || year > 99) { /* error exit */ }

    and write the output statement as

    printf("Dated this %d%s day of %s, %d.\n",
    day, ord(day), month_name[month - 1], year + 2000);

    which I think makes the assumptions about the year clearer than burying
    "20%.2d" in the printf format.

    (or even add "year += 2000;" after the validity check)
    --
    Morris Keesan --
    Morris Keesan, Dec 23, 2011
    #8
  9. flebber

    Tim Rentsch Guest

    Ben Bacarisse <> writes:

    > "BartC" <> writes:
    >
    >> "Ben Bacarisse" <> wrote in message
    >> news:...

    > <snip>
    >>> const char *ord[] = { "th", "st", "nd", "rd", "th",
    >>> "th", "th", "th", "th", "th" };
    >>> ...
    >>> printf("Dated this %d%s day of %s, 20%.2d.\n",
    >>> day, ord[day % 10], month_name[month - 1], year);
    >>>
    >>> Apart from being neater, it has the advantage of not breaking up the
    >>> output with an intervening switch statement. You can keep the format
    >>> string in one piece.

    >>
    >> I haven't tried this, but does it work for day 11 and day 12 too?

    >
    > No, you are absolutely right, it does not work for those. The obvious
    > fix would be 20 strings, but that's getting ugly. There is probably
    > some more obscure expression that can cut down the number of strings
    > needed but it will start to lose clarity.


    If you will forgive a small indulgence...

    static inline const char *
    day_in_month_suffix( int d /* d in [0..31) */ ){
    return (char*[]){"th","st","nd","rd"}[ (int[]){1,2,3,[20]=1,2,3,[30]=1}[d] ];
    }
    Tim Rentsch, Jan 26, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    1,720
    Ilias Lazaridis
    Dec 27, 2004
  2. White Wolf

    Temporary King

    White Wolf, Jan 25, 2005, in forum: C++
    Replies:
    5
    Views:
    395
    Mike Wahler
    Jan 25, 2005
  3. tek monk
    Replies:
    4
    Views:
    422
    onetitfemme
    Sep 15, 2006
  4. The New King of Pop(TYG)

    , Aug 8, 2007, in forum: C Programming
    Replies:
    0
    Views:
    321
  5. flebber

    King - Chapter 5 date.c

    flebber, Dec 20, 2011, in forum: C Programming
    Replies:
    13
    Views:
    612
    James Kuyper
    Dec 21, 2011
Loading...

Share This Page