Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > King - Chapter 5 date.c

Reply
Thread Tools

King - Chapter 5 date.c

 
 
flebber
Guest
Posts: n/a
 
      12-20-2011
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]
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      12-20-2011
On 2011-12-20, flebber <(E-Mail Removed)> wrote:
> scanf("%d / %d /%d" &month &day &year);


You're missing commas.

> case1: case21: case31:


You're missing spaces.

The problem is that "case 1" is a component of a switch statement, which
can be reused in each switch statement, but "case1" is a label, which cannot
be reused within the same function.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
 
flebber
Guest
Posts: n/a
 
      12-21-2011
On Dec 21, 10:13*am, Seebs <(E-Mail Removed)> wrote:
> On 2011-12-20, flebber <(E-Mail Removed)> wrote:
>
> > * * * *scanf("%d / %d /%d" &month &day &year);

>
> You're missing commas.
>
> > * * * * * * * *case1: case21: case31:

>
> You're missing spaces.
>
> The problem is that "case 1" is a component of a switch statement, which
> can be reused in each switch statement, but "case1" is a label, which cannot
> be reused within the same function.
>
> -s
> --
> Copyright 2011, all wrongs reversed. *Peter Seebach / (E-Mail Removed)://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
> I am not speaking for my employer, although they do rent some of my opinions.


Thank You all for the responses.

Gordon I did want to clarify

> You told the user to enter a *4-digit* year (good move) and now
> you're going to discard the century entered and use 20?


The book states that the 20%.2d has to be used as otherwise a year
such as 2005 would be printed as 205.

Sayth
 
Reply With Quote
 
flebber
Guest
Posts: n/a
 
      12-21-2011
On Dec 21, 10:13*am, Seebs <(E-Mail Removed)> wrote:
> On 2011-12-20, flebber <(E-Mail Removed)> wrote:
>
> > * * * *scanf("%d / %d /%d" &month &day &year);

>
> You're missing commas.
>
> > * * * * * * * *case1: case21: case31:

>
> You're missing spaces.
>
> The problem is that "case 1" is a component of a switch statement, which
> can be reused in each switch statement, but "case1" is a label, which cannot
> be reused within the same function.
>
> -s
> --
> Copyright 2011, all wrongs reversed. *Peter Seebach / (E-Mail Removed)://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
> I am not speaking for my employer, although they do rent some of my opinions.


And yes playing with the program very easy to break.

Enter the date mm/dd/yyyy: 06/28/2005
Dated this 28th day of June, 202005.

Enter the date mm/dd/yyyy: 28/06/1974
Dated this 6th day of , 201974.

Sayth
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      12-21-2011
On Tue, 20 Dec 2011 20:50:59 -0800 (PST), flebber
<(E-Mail Removed)> wrote:

>On Dec 21, 10:13*am, Seebs <(E-Mail Removed)> wrote:
>> On 2011-12-20, flebber <(E-Mail Removed)> wrote:
>>
>> > * * * *scanf("%d / %d /%d" &month &day &year);

>>
>> You're missing commas.
>>
>> > * * * * * * * *case1: case21: case31:

>>
>> You're missing spaces.
>>
>> The problem is that "case 1" is a component of a switch statement, which
>> can be reused in each switch statement, but "case1" is a label, which cannot
>> be reused within the same function.
>>
>> -s
>> --
>> Copyright 2011, all wrongs reversed. *Peter Seebach / (E-Mail Removed)://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
>> I am not speaking for my employer, although they do rent some of my opinions.

>
>And yes playing with the program very easy to break.
>
>Enter the date mm/dd/yyyy: 06/28/2005
>Dated this 28th day of June, 202005.
>
>Enter the date mm/dd/yyyy: 28/06/1974
>Dated this 6th day of , 201974.
>


I don't have a copy of this book. Are you sure it expects a 4-digit
year? Putting a 20 in front of a 2-digit year would only be valid in
that case. It also limits the function to years 2000-2099, hardly a
modern approach.

I also question the lack of a default case in the second switch block.
This is a serious deficiency in a tutorial that is supposed to be
demonstrating "a modern approach" to C.

Is there an online version of this example?

It appears to be K.N. King, "C Programming : A Modern Approach" page
89.
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      12-21-2011
On Tue, 20 Dec 2011 20:43:05 -0800 (PST), flebber
<(E-Mail Removed)> wrote:

>The book states that the 20%.2d has to be used as otherwise a year
>such as 2005 would be printed as 205.


This is untrue for this function but it also sounds like a Perl-ism.

The last time I worked with perl it used years based on 1900 so you
were always adding it to output 4 digit years.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      12-21-2011
On 2011-12-21, Geoff <(E-Mail Removed)> wrote:
> This is untrue for this function but it also sounds like a Perl-ism.


> The last time I worked with perl it used years based on 1900 so you
> were always adding it to output 4 digit years.


Er. That's a C-ism. Which perl adopted because perl had a transparently
thin layer over C for a lot of functions. See also "struct tm".

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
André Gillibert
Guest
Posts: n/a
 
      12-21-2011
Geoff <(E-Mail Removed)> wrote:
> On Tue, 20 Dec 2011 20:50:59 -0800 (PST), flebber
> >Enter the date mm/dd/yyyy: 06/28/2005
> >Dated this 28th day of June, 202005.
> >
> >Enter the date mm/dd/yyyy: 28/06/1974
> >Dated this 6th day of , 201974.
> >

>
> I don't have a copy of this book. Are you sure it expects a 4-digit
> year? Putting a 20 in front of a 2-digit year would only be valid in
> that case. It also limits the function to years 2000-2099, hardly a
> modern approach.
>
> I also question the lack of a default case in the second switch block.
> This is a serious deficiency in a tutorial that is supposed to be
> demonstrating "a modern approach" to C.
>
> Is there an online version of this example?
>
> It appears to be K.N. King, "C Programming : A Modern Approach" page
> 89.


That's a pretty modern approach.
1) It wouldn't have worked in 1990's.
2) It's Y2K "compliant"

So, the book has been written after year 2000.

Anyway, date handling is tough, and requires knowledge of timezones,
daylight saving time, calendars, leap years and leap seconds and rule
changes.
This is one of the programming fields where bug-free implementations are
the most rare. In a programming book, such a basic example should be
done right. Students tend to re-use wrong code from half-baked examples.

--
André Gillibert
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-21-2011
flebber <(E-Mail Removed)> writes:
> On Dec 21, 10:13Â*am, Seebs <(E-Mail Removed)> wrote:

[...]
>> You told the user to enter a *4-digit* year (good move) and now
>> you're going to discard the century entered and use 20?

>
> The book states that the 20%.2d has to be used as otherwise a year
> such as 2005 would be printed as 205.


I suspect you've misunderstood what the book says.

If the year value is a 2-digit year restricted to the years 2000-2099,
then printing it with "20%.2d" should work. If you print it with
"20%d", it won't print a leading 0 for values less than 10, so a year
entered as 5 will print as "205" rather than the correct "2005".

In some contexts, it's common to represent as year as the number of
years since 1900 (this is how the tm_year member of struct tm, defined
in <time.h>, works). If you were using that representation, you
wouldn't want to prepend either "19" or "20" to the year; you'd want to
add 1900 to it.

But as Seebs says, your prompt is:

printf("Enter the date mm/dd/yyyy: ");

which asks for a 4-digit year. The user is going to enter "2005", not
"05" or "5".

Perhaps the book intends the prompt to be:

printf("Enter the date mm/dd/yy: ");

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      12-21-2011
On Wed, 21 Dec 2011 11:35:16 +0100, André Gillibert
<(E-Mail Removed)> wrote:

>Geoff <(E-Mail Removed)> wrote:
>> On Tue, 20 Dec 2011 20:50:59 -0800 (PST), flebber
>> >Enter the date mm/dd/yyyy: 06/28/2005
>> >Dated this 28th day of June, 202005.
>> >
>> >Enter the date mm/dd/yyyy: 28/06/1974
>> >Dated this 6th day of , 201974.
>> >

>>
>> I don't have a copy of this book. Are you sure it expects a 4-digit
>> year? Putting a 20 in front of a 2-digit year would only be valid in
>> that case. It also limits the function to years 2000-2099, hardly a
>> modern approach.
>>
>> I also question the lack of a default case in the second switch block.
>> This is a serious deficiency in a tutorial that is supposed to be
>> demonstrating "a modern approach" to C.
>>
>> Is there an online version of this example?
>>
>> It appears to be K.N. King, "C Programming : A Modern Approach" page
>> 89.

>
>That's a pretty modern approach.
>1) It wouldn't have worked in 1990's.
>2) It's Y2K "compliant"
>
>So, the book has been written after year 2000.
>
>Anyway, date handling is tough, and requires knowledge of timezones,
>daylight saving time, calendars, leap years and leap seconds and rule
>changes.
>This is one of the programming fields where bug-free implementations are
>the most rare. In a programming book, such a basic example should be
>done right. Students tend to re-use wrong code from half-baked examples.


I agree. After a google search I found this:
http://www.cs.unb.ca/~bremner/teachi...amples/date.c/

If this is a faithful transcription of the code in the book then it
explains the last printf of the function. The original only expected a
2-digit year and flebber copied it wrong.

For an example of the use of the switch and case statements it serves
as fair example but it's a pretty fragile function since it doesn't
validate user input and will fail if day > 31 || day <= 0 || month <=
0 || month > 12 or if the year is expressed as a "modern" 4-digit year
as many people are conditioned to input in the 21st century.

Sanity checking those values immediately after input would justify
leaving out case coverage in the 2nd switch. Perhaps it's an exercise
later in the chapter to make it more robust.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
King - Chapter 5 date.c flebber C Programming 8 01-26-2012 12:11 AM
King King DVD - which poster? janey3white@yahoo.com DVD Video 4 11-23-2005 04:32 PM
70-305, Chapter 5 (ADO): Login failed Irene MCSD 9 02-13-2004 03:50 AM
Cisco Networking Simplified - book review and a sample chapter Michael Cisco 0 11-19-2003 11:09 PM
107-188 class, Chapter 1 Step 5 Hallema Morgan Microsoft Certification 0 08-26-2003 11:26 PM



Advertisments