[prog] C problem I am unable to solve

Angelina Carlton brat at magma.ca
Thu May 12 06:42:23 EST 2005


On Wed, May 11, 2005 at 08:29:42PM +0100, Dan wrote:
> Your problem may be here:
> 
> >         scanf("%c", &q);
> 
> See if it's reading the newline (Return or Enter) character as well as
> the character you're looking for.
> 

Thank you I tired adding a newline or space in the scanf format
specifier but it wanted an aditional keypress to quit, ie, q q

This does work though:
 
 while(! (feof(inputfile)) ) { /* until the end of the file*/
    for (i = 0; i <= 19; ++i) {
      while ( (c = getc(inputfile)) != '\n' )
        putc(c, stdout); /* display the line */
      putc('\n', stdout);
      if(i == 19) { /* stop at 20 */
        printf("press 'q' to quit, or [ENTER] to continue:\n");
        while ((q = getchar()) != '\n' && q != EOF && (q == 'q')) {
          return 0;
        }
      }
    }
  }
  return 0;

If you press q, it quits right away, if you press enter, it continues
with the next 20 lines which is great, but if you press anyother key
it displays 40 lines

        while ((q = getchar()) != '\n' && q != EOF  ) {

this line works with "press [ENTER] to continue, any other key to
quit." which is good enough I think, not exactly what the book is
asking but it only will accept [ENTER] to print the next 20 lines and
predictably quits with any other key.

I am not in a class or school so have no proffessor to justify this to
so I can live with it and now move on to try to fix the infinate loop
at the end :-)
  
 


--
Angelina Carlton


More information about the Programming mailing list