checking input

Post your C programming questions here.

checking input

Postby cws » Tue Mar 02, 2010 12:38 pm

I just finished LearnC on the Mac last night and now I am fiddling with some of the routines to achieve different results.

I am trying to write a routine which will check the input of the user and limit that input to the length, kMaxInputLength. After I get the length check working I intend to add a check for certain values, such as kUpperCase, kNumbers, kAnyAlpha or whatever I want to define.

If I enter fewer than kMaxInputLength, it prints the 'OK' message, the Goodbye message and exits with status 0.

If I enter too many characters, then the code prints the 'Too Long' message and asks again for input until I enter input which is less than kMaxInputLength.

All seems well. However after entering an acceptable number of characters, I get the OK message, the Goodbye message but then the following error.

Code: Select all
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all

I have to stop the debugger. I figure that a flag was raised when I exceeded the limits of yourInput[ kMaxInputLength ]. But I cant figure out how to correct this problem.

Thanks,

Willie

Code: Select all
#include <stdio.h>
#include <string.h>      //brings in strlen()

#define kMaxInputLength      15
#define kZeroByte         0

void   GetInput( char *yourInput );
void   CheckInput( char *yourInput );
void   ReadLine( char *yourInput );

int      gNumChars;

//**************************************************
int main (int argc, const char * argv[])
{
   char   yourInput[ kMaxInputLength ];

   GetInput( yourInput );

   printf("Goodbye!\n");
   
   return 0;
}


//**************************************************
void   GetInput( char *yourInput )
{
   do {
      printf( "\nType something, please:  " );
      
      ReadLine( yourInput );
      
      gNumChars = strlen( yourInput );

      //for testing
      printf( "\nYou typed '%s' which has %d character", yourInput, gNumChars );
      
      if ( gNumChars != 1 )
         printf( "s" );
            
      CheckInput( yourInput );
   }
   while ( kMaxInputLength <= gNumChars );
}


//**************************************************
void   ReadLine( char *yourInput )
{
   while ( (*yourInput = getchar()) != '\n' )
      yourInput++;
   
   *yourInput = kZeroByte;
}


//**************************************************
void   CheckInput( char *yourInput )
{
   if ( gNumChars < kMaxInputLength )
      printf("\nThe length of the input is OK.\n");
   else {
      printf( "\nThe length of input is too long!\n" );
   }
}
cws
 
Posts: 1
Joined: Sat Feb 20, 2010 8:39 pm

Return to Questions about C programming...

Who is online

Users browsing this forum: No registered users and 1 guest