Jump to content
Larry Ullman's Book Forums

How To Test For Enter In Scanf - Script 11.5

Recommended Posts

Hi Larry,


I am up to page 277, script 11.5 called copy.c.


I am interested how you could test for just a keyboard 'enter' as well as testing for a zero (about code line 30).  I have Googled a bit but nothing seems to work. 


I have tried testing the scanf return code but I can't seem to get that to work either.


I am using a relatively recent version of Dev-C++ as the IDE in Windows 7 64 bit.


Any help will be most appreciated.


Thanks, and cheers from Oz.

Link to comment
Share on other sites

I think that I now understand why my test for a return code from scanf does not work.


Here's my code:

 /* Get up to NUM_STRINGS words. */

 for (i = 0; i < NUM_STRINGS; ++i) {


  /* Prompt and read in word. */

  printf("Enter a word (or 0 to quit): ");


  rc = scanf("%9s", input);

  printf("The return code from scanf is %d\n", rc);

The retrun code (rc) printf does not show at all if all I enter is 'enter' at the scanf. And on page 87 Larry says that scanf will not work "if the function encounters input that does not match the formatting signifier...".  But I still don't understand why the printf would not show anything?


Again, any advice will be most appreciated.


Link to comment
Share on other sites

Mmmm, I think "the penny just dropped".  This is an expression used here in Oz to indicate that you just realised something that you should have realised before.  I wonder if the expression is used in North America?


It seems that the scanf is just ignoring my keyboard 'enters' and is still waiting for some input that it is prepared to accept.  Which is why it never gets to the following printf.  And also why it will accept a word even after several 'enter's. And once it receives a word, then the printf 'fires'.


Which still leaves my query open that I hope someone can help me with - is there a way to test for a keyboard enter in the context of this code?


Thanks again.

Link to comment
Share on other sites

Ok, I've got the solitary enter bit sorted as per:

/* Get up to NUM_STRINGS words. */

 for (i = 0; i < NUM_STRINGS; ++i)


  /* Prompt and read in a word. */

  printf("Enter a word up to %d characaters in length (or 0 or 'enter' to quit): ", STR_LEN-1);

  fgets(input, STR_LEN-1, stdin);


    //  Now test for a solitary newline character or a zero

      if ((((p = strchr(input, '\n')) != NULL) && strlen(input) == 1) || input[0] == '0')

        { // i.e., the characater entered was a solitary keyboard 'enter' or a zero


 // now replace the new line character with an end of string zero (\0)
  if ((p = strchr(input, '\n')) != NULL)
        *p = '\0';  }

  strncpy(words[i], input, (STR_LEN - 1));



Where p is declared as char *p; But now I have to hit 'enter' twice to progress to the request for the next input.  I'll keep trying!!



Link to comment
Share on other sites

I had another of those 'penny dropped' moments.  The test for the solitary 'enter' above is too complicated; all you need is:


if (input[0] == '\n' || input[0] == '0')


Oh well, one lives and learns!



Link to comment
Share on other sites


  • Create New...