Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #60762

Re: can this be done recursively

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: can this be done recursively
Date 2015-04-22 13:29 -0400
Organization Self
Message-ID <5537DA7C.5050806@verizon.net> (permalink)
References <b0319922-1b5e-48d2-8cc7-5cd1a5e866e6@googlegroups.com> <e07362d0-5d7e-4243-acc6-bcf384298b69@googlegroups.com>

Show all headers | View raw


On 04/22/2015 12:42 PM, G G wrote:
> I have another question: i'm using os x 10.3.3, and when i do a control-D
> the program does not quit. it enters an infinite loop, well, a loop, it seems
> as if it is infinite, of course i stop it, using Xcode stop execution button.
> 
> shouldn't control-D or control-z halt the execution of the program no matter
> what's being read in?  i have not disable the control-D as far as i know.

That's something for OS X to define, and I don't know anything about
that. However, there's some obvious problems with your code that iare
almost certainly related to that issue.


>     while ( programStatus == CONTINUE )
>     {
>         /* prompt user to enter an integer */
>         printf( "Enter an integer to determine if it is even or odd : " );
>         
>         /* read in integer */
>         scanf( "%ld", &number );
>         ch = getchar();          /* need to handle the return when pressed */

The value returned by scanf() would be 1 if the number is successfully
read, 0 if the input stream cannot be interpreted as an integer, and EOF
if an I/O error occurred or if end-of-file occurs. Your code doesn't
check for any of those possibilities.
Note: you can use feof(stdin) and ferror(stdin) to distinguish those
last two possibilities, if you care about the distinction.

>         
>         if ( even( number ) )
>             printf( "\n%ld is even\n\n", number );
>         else
>             printf( "\n%ld is odd\n\n", number );
>         
>         ch = ' ';               /* make sure ch does not contain the \n character */
>         
>         puts("another number, y for yes, n for no?");
>         
>         
>         /* allow only y,Y,n or N to control program continuation or termination */
>     
>         while ( ( (ch = getchar()) != '\n') || (characterOK == NO) )

getchar() returns EOF if there's an end-of-file or an I/O error. Again,
feof(stdin) and ferror(stdin) can be used to distinguish those cases.
Again, your code does nothing to check for either of those possibilities.

Obscure note: in the extremely unlikely event that UCHAR_MAX > INT_MAX
(which would require that CHAR_BIT >= 16), then getchar() can also
return EOF as a result of an ordinary read of a byte of data containing
the value (unsigned char)EOF. On such a platform, you'll need to check
both feof(stdin) and ferror(stdin) before deciding whether or not an
error occurred.

>         {
>             char temp = ch;   /* needed because of the return, '\n', character */
>             
>             switch( temp )
>             {
>                 case 'y':
>                 case 'Y':
>                     programStatus = CONTINUE;
>                     characterOK = YES;
>                     break;
>                 
>                 case 'n':
>                 case 'N':
>                     programStatus = FINISHED;
>                     characterOK = YES;
>                     break;
>                     
>                 default:
>                     puts("another number, y for yes, n for no?");
>                     characterOK = NO;
>                     ch = getchar();

The same issue applies here.

>                     ch = ' ';
>                     break;
>                     
>             } /* end switch( temp ) */
>             
>         } /* end while ( ( (ch = getchar() ... ) */

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 09:50 -0700
  Re: can this be done recursively Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-19 21:14 +0100
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:20 -0700
  Re: can this be done recursively Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-04-19 16:32 -0400
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:25 -0700
  Re: can this be done recursively Keith Thompson <kst-u@mib.org> - 2015-04-19 14:09 -0700
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:35 -0700
      Re: can this be done recursively jt@toerring.de (Jens Thoms Toerring) - 2015-04-19 22:57 +0000
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-20 04:30 -0700
    Re: can this be done recursively Richard Heathfield <rjh@cpax.org.uk> - 2015-04-20 12:49 +0100
      Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-20 05:28 -0700
      Re: can this be done recursively jt@toerring.de (Jens Thoms Toerring) - 2015-04-20 13:09 +0000
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-22 09:42 -0700
    Re: can this be done recursively Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-22 10:12 -0700
    Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 13:29 -0400
    Re: can this be done recursively Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-22 20:57 +0100
      Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 16:12 -0400
        Re: can this be done recursively Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-22 23:14 +0100
          Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 18:56 -0400
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-23 07:59 -0700

csiph-web