Hey folks,
I have first concentrated on writing functional code that does what I want. However, I have read the FAQ and numerous posts about portability issues with fflush so I have now attempted to replace it with some more reliable, if more complex, code in the program I'm working on.
Here's what I had. These 3 lines are simple and effective for my purposes:
And here's the best mess I've come up with to replace it:Code:printf ("\nEnter a new name.\n\n>"); scanf ("%[^\n]", entry.name); fflush(stdin);
I commented what I think it should be/want it to be doing. However, no matter what I enter, the if statements seem to always == NULL. I think this has something to do with the EOF because when I remove it the behavior changes. When I compile with only the newline in the if statements the first if works as I intend but I'm still getting junk from over-length entries in the name field output and the correct second entry from the "try again" is no where to be found.Code:printf ("\nEnter a new name.\n\n>"); fgets(entry.name, sizeof(entry.name), stdin); if (strchr (entry.name, '\n' && EOF) == NULL) /* if overflow */ { printf ("\nYour entry is too long, please try again."); while (strchr (entry.name, '\n' && EOF) == NULL) /* keep reading until */ { /* stdin is empty */ fgets(entry.name, sizeof(entry.name), stdin); } input (); /* recursive */ } entry.name[strlen(entry.name) - 1] = ' '; /* replace \n */
Aside from actually fixing the code above, I'm open to any suggestions on how to streamline this as there's likely a much simpler way to do it. Thanks in advance for suggestions and insights. Cheers.
-SH



LinkBack URL
About LinkBacks




