Hello. I am writing a program using array notation in which a random string of 40 characters is generated (s1), a user is allowed to enter a string of characters between 2 and 20 (s2), and replacement character (c). A sample output of this program is:
s1=PHQGHUMEAYLNLFDXFIRCVSCXGGBWKFNQDUXWFNFO
Enter a string of characters of length 2 and 20:
JOW
Enter a replacement char.
*
s2=JOW
filtered s1=PHQGHUMEAYLNLFDXFIRCVSCXGGB*KFNQDUX*FNF*
I have the program running fine as shown above, but I am having trouble with error checking. The user cannot enter a string less than 2 characters long or longer than 20 characters. A user cannot enter a string with a non-capital letter (i.e. "a9&" is incorrect). My error checking output will do this:
s1=PHQGHUMEAYLNLFDXFIRCVSCXGGBWKFNQDUXWFNFO
Enter a string of characters of length 2 and 20:
J
Error in string.
Enter a string of characters of length 2 and 20:
JKL
Enter a replacement char.
*
Enter a replacement char.
s2=JKL
filtered s1=PHQGHUMEAY
N
FDXFIRCVSCXGGBW
FNQDUXWFNFO
Would you like to continue?
y
Press any key to continue . . .
My code for error checking is within the user entered string function:
The clearBuffer() function code is as follows:Code:char userEntry(char s2[]){ char str2[100]; char ch, c, temp; int error; int i=0, j=0, length=0; puts("Enter a string of characters of length 2 and 20:"); gets(str2); length=string_length(str2); if(length<2||length>20){ error=1; } else if(str2[1]=='\n'||str2[1]=='\0'){ error=1; } else{ for(i=0;i<length;i++){ temp=str2[i]; s2[i]=temp; if(!isCapital(s2[i])){ error=1; break; } s2[length]='\0'; } } if(error==1){ printf("Error in string.\n"); clearBuffer(); userEntry(s2); } puts("Enter a replacement char."); ch=getchar(); return ch; }
Is my attempt to re-enter the user string s2 by calling userEntry(s2) after clearBuffer() what is causing such a wacky output? Any help would be greatly appreciated.Code:char clearBuffer(){ int ch; char buf[BUFSIZ]; while((ch=getchar())!='\n'&&ch!=EOF); return 0; }



LinkBack URL
About LinkBacks


