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" );
}
}
