Back to DFS's C Page


OAC Problem Set VII

Bulletproofing
Due: Beginning of Class on 8 Dec 99

Below you will find three versions of a function designed to obtain a positive integer from the user. All three include attempts to bulletproof the code, but they all fail.

  1. scanf()
  2. gets()
  3. fgets()

With your partner from the previous problem set, you are to use each version in a copy of the program in Functions III and determine under what circumstances there are difficulties.

In point form, you are to write up a report of your findings, which should include pertinent information from the man pages.

Finally, after consulting the appropriate man pages, you are to finish bulletproofing the fgets() version.

scanf() Version

Back to Top

void GetPositiveInteger( int *newinteger, char *whichvar)
{
   /* USING scanf() */
   int badinputflag;
   char dummystr[25];

   do {
      badinputflag = scanf("%d", newinteger);
      if (badinputflag == 0)
      {
         scanf("%s", dummystr); /* Clear the input buffer */
         printf("A typo was found for %s.\n", whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
      }
      else if (*newinteger <= 0)
      {
         printf("The %s needs to be GREATER than zero.\n", whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
      }
   } while ( (badinputflag == 0) || (*newinteger <= 0) );
} /* GetPositiveInteger */

gets() Version

Back to Top

void GetPositiveInteger( int *newinteger, char *whichvar)
{
   /* USING gets() */
   char numstr[16];
   char *goodinputflag;
   char *badcharptr;
   do {
      goodinputflag = gets(numstr);
      if (!goodinputflag || !strlen(numstr))
      {
         goodinputflag = NULL;
         printf("A problem was found with %s.\n", whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
         continue;
      }          
      *newinteger = strtol(numstr, &badcharptr, 10);
      if (*badcharptr != '\0')
      {          
         printf("A typo, %c, was found for %s.\n", *badcharptr, whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
      }          
      else if (*newinteger <= 0)
      {          
         printf("The %s needs to be GREATER than zero.\n", whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
      }          
   } while ( !goodinputflag || *badcharptr || (*newinteger <= 0) );
} /* GetPositiveInteger */

fgets() Version

Back to Top

void GetPositiveInteger( int *newinteger, char *whichvar)
{
   /* USING fgets() in a buggy fashion */
   char numstr[32];
   char *goodinputflag;
   char *badcharptr;
   do {
      goodinputflag = fgets(numstr, 32, stdin);
      if( numstr[strlen(numstr) - 1] == '\n')
         numstr[strlen(numstr) - 1] = '\0';
      if (!goodinputflag || !strlen(numstr))
      {
         goodinputflag = NULL;
         printf("A problem was found with %s.\n", whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
         continue;
      }
      *newinteger = strtol(numstr, &badcharptr, 10);
      if (*badcharptr != '\0')
      {
         printf("A typo, %c, was found for %s.\n", *badcharptr, whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
      }
      else if (*newinteger <= 0)
      {
         printf("The %s needs to be GREATER than zero.\n", whichvar);
         printf("Please re-enter a value for the %s: ", whichvar);
      }
   } while ( !goodinputflag || *badcharptr || (*newinteger <= 0) );
} /* GetPositiveInteger */

Created 5 Dec 1999
© 1999 DFStermole