Thread: Problem with clearing the buffer and user entered string s2

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Problem with clearing the buffer and user entered string s2

    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:
    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;
    }
    The clearBuffer() function code is as follows:
    Code:
    char clearBuffer(){
    	int ch;
    	char buf[BUFSIZ];
    	
    	while((ch=getchar())!='\n'&&ch!=EOF);
    	return 0;
    }
    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first thing to do is to STOP using gets()
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

    gets() will NEVER leave a \n on the input stream for you to find using clearBuffer(). gets() will instead trash memory in its attempt to find the \n at the end of an input line.

    I would suggest
    - fgets() to read a line into a large buffer
    - if it has no \n, then call your clearBuffer function
    - validate the buffer for length and content; if it's OK, copy it to your result
    - fgets() to read a line into a large buffer
    - if it has no \n, then call your clearBuffer function
    - return the first character as your return result character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    why dont you use the function strlen() to know the length of the input string?
    if its less than two or greater than 20 then report error.
    simple.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    I can't use any string handling functions. I wrote my own version of strlen() which is working fine. My new code looks like this now, but it's printing out the error message even when a valid string is entered.
    Code:
    char userEntry(char s2[]){/*begin userEntry*/
    	char str2[21];
    	char ch, c, temp;
    	int error=0;
    	int i=0, j=0, length=0;
    
    	puts("Enter a string of characters of length 2 and 20:");
    	fgets(str2,21,stdin);
    	length=string_length(str2);
    	printf("%d\n",length);
    	if(length<2||length>20){
    		error=1;
    	}
    	else if(str2[1]=='\n'||str2[1]=='\0'){
    		error=1;
    	}
    
    	else if(length>2&&length<20){
    		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;
    }/*end userEntry*/
    Output:
    s1=PHQGHUMEAYLNLFDXFIRCVSCXGGBWKFNQDUXWFNFO
    Enter a string of characters of length 2 and 20:
    JIO
    4
    Error in string.

    Enter a string of characters of length 2 and 20:
    JIOW
    5
    Error in string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clearing the buffer
    By Brian_Jones in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2009, 11:36 PM
  2. Clearing Buffer Problem (beginners Q)
    By Iconate in forum C Programming
    Replies: 5
    Last Post: 03-18-2008, 01:57 PM
  3. clearing buffer after reading string w/ scanf()
    By fisheromen1031 in forum C Programming
    Replies: 11
    Last Post: 08-01-2005, 09:33 AM
  4. Clearing Buffer
    By AndyBomstad in forum C++ Programming
    Replies: 11
    Last Post: 04-30-2005, 01:04 AM
  5. Clearing the buffer
    By caduardo21 in forum C Programming
    Replies: 3
    Last Post: 02-04-2005, 04:29 PM

Tags for this Thread