Xref: utzoo misc.misc:6113 comp.misc:6072 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!mit-eddie!bloom-beacon!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Newsgroups: misc.misc,comp.misc Subject: Re: The "evil" GOTO (Was: 25 Years of BASIC) Message-ID: <11346@bloom-beacon.MIT.EDU> Date: 11 May 89 19:33:59 GMT References: <1791@ubu.warwick.UUCP> <1436@onion.reading.ac.uk> <1814@ubu.warwick.UUCP> <1860@ubu.warwick.UUCP> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: jik@athena.mit.edu (Jonathan I. Kamens) Followup-To: comp.misc Organization: Massachusetts Institute of Technology Lines: 65 In article <1860@ubu.warwick.UUCP> geoff@cs.warwick.ac.uk (Geoff Rimmer) writes: >Here is my solution. Which does the same as that of the original >poster, and in addition: > >(a) it checks the buffer overflowing; >(b) it checks for EOF >(c) it is correct C code Actually, it is not correct C code for six reasons. In fact, it won't even compile. It's not very intelligent in an argument like this to be all high-and-mighty and post a code fragment that you claim is "correct C code" when it won't even compile. Here's the code you posted: >#include > >main() >{ > char str[4]; > printf("Enter your sex: "); > do > { > fflush(stdin); > if (!fgets(str,3,stdin)) exit(1); > } while (*str!="m" && *str!="f") ? printf("m or f only: "):0); > printf("sex is %c\n",*str); >} 1. You aren't flushing stdout after printing the prompt, and the prompt does not end in a newline, so in many operating systems the prompt will not be printed. 2. You flush stdin, but, to quote from the man page: Fflush causes any buffered data for the named output stream to be written to that file. The stream remains open. In other words, fflush is not defined for input files, and stdin is input-only. 3. You assume that printf will return a non-zero value. Now, I don't know about your implementation of printf, but mine (BSD 4.3) doesn't spec what printf returns, and I don't think that most implementations ahve such a specification (although I believe that ANSI says printf should return the number of characters it's printed). In fact, I just compiled your program (with the two bugs below fixed, since it won't compile otherwise), and our IBM RT library seems to return 0 after a successful printf, so your code loses. 4. You're missing a parenthesis at the beginning of your while boolean expression. 5. You're comparing the value of a char to the value of a string pointer in your boolean expression. It should be (*str!='m' && *str!='f') rather than what you have. 6. It doesn't print an error message if the fgets fails. Now, granted, this isn't really "incorrect C," but rather "incorrect programming," but it should be pointed out nevertheless. Oh, and one more thing: what the hell good is a program that does nothing but get somebody's sex and print it? :-) Jonathan Kamens USnail: MIT Project Athena 410 Memorial Drive, No. 223F jik@Athena.MIT.EDU Cambridge, MA 02139-4318 Office: 617-253-4261 Home: 617-225-8218