I think I am not flushing my buffer correctly. I'm trying to check two things in this function. I can get each piece to work independantly but throwing them together is giving me a headache. First I'm checking to make sure the char read is an alpha and second I'm checking to make sure it is less than STR_MAX.
Code:void A_getOriginalString() { int i = 0; char ch; printf("Enter a word, no more than 20 alpha characters only, or ? to quit: "); while ((ch = getchar()) && ch != '\n') { if (isalpha(ch)) { originalString[i] = ch; i++; if (i > 20) { printf("You entered more than 20 characters. Please try again.\n"); printf("Enter a word, no more than 20 alpha characters only, or ? to quit: "); fflush(stdin); i = 0; ch = getchar(); } } else { printf("You entered a non-alpha character. Please try again.\n"); printf("Enter a word, no more than 20 alpha characters only, or ? to quit: "); fflush(stdin); i = 0; ch = getchar(); } originalString[i] = '\0'; } }



LinkBack URL
About LinkBacks


