Necuima 18 Posted February 25, 2015 Report 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. Quote Link to post Share on other sites
Necuima 18 Posted February 25, 2015 Author Report 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. Quote Link to post Share on other sites
Necuima 18 Posted February 26, 2015 Author Report 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. Quote Link to post Share on other sites
Necuima 18 Posted February 26, 2015 Author Report 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. Quote Link to post Share on other sites
Necuima 18 Posted February 26, 2015 Author Report Share Posted February 26, 2015 Ok, got it - it seems that you don't need the 'discard_input()' with fgets. :-) Quote Link to post Share on other sites
Larry 428 Posted February 28, 2015 Report Share Posted February 28, 2015 Ha! Fun watching this develop! Kudos for figuring it out and thanks for sharing. Quote Link to post Share on other sites
Necuima 18 Posted March 1, 2015 Author Report 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 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.