Necuima Posted February 25, 2015 Share Posted February 25, 2015 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 More sharing options...
Necuima Posted February 25, 2015 Author Share Posted February 25, 2015 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 More sharing options...
Necuima Posted February 26, 2015 Author Share Posted February 26, 2015 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 More sharing options...
Necuima Posted February 26, 2015 Author Share Posted February 26, 2015 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); discard_input(); // 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 break; // 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)); count++; } 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!! Cheers. Link to comment Share on other sites More sharing options...
Necuima Posted February 26, 2015 Author Share Posted February 26, 2015 Ok, got it - it seems that you don't need the 'discard_input()' with fgets. :-) Link to comment Share on other sites More sharing options...
Larry Posted February 28, 2015 Share Posted February 28, 2015 Ha! Fun watching this develop! Kudos for figuring it out and thanks for sharing. Link to comment Share on other sites More sharing options...
Necuima Posted March 1, 2015 Author Share Posted March 1, 2015 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! Cheers Link to comment Share on other sites More sharing options...
Recommended Posts