Thread: clearthe buffer

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    Unhappy clearthe buffer

    I am trying to clear the buffer with the while(getchar() != '\n') ; but it does not appear to be clearing. What am I doing wrong?


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define str_length 100
    void main()
    {
    
    	char str1[100];			/* array for first string */
    	char str2[100];			/* array for second string */
    	char *pstr1 = NULL;	/* pointer to str1 */
    	char *pstr2 = NULL;	/* pointer to str2 */
    	int count1;				/* length of str1 */
    	int count2;				/* length of str2 */
    	char more = 'y' ;				/* variable to continue with the loop y||Y */
    	int i = 0;				/*loop counter */
    	char *pbuffer1 =str1;
    	char *pbuffer2 =str2;
    
    	printf("Please enter the first word\n");
    	pstr1 = pbuffer1;
    
    	count1 = strlen(pstr1);
    
    	while((*pbuffer1++ = getchar()) != '\n');
    
    	*(pbuffer1 - 1) = '\0';
    
    	printf("Please enter the second word\n");
    
    	pstr2 = pbuffer2;
    	count2 = strlen(pstr2);
    
    	while((*pbuffer2++ = getchar()) != '\n');
    	
    	*(pbuffer2 - 1) = '\0';
    
    
    	do
    	{
    	/* concatenate str2 to end of str1 */
    	if(count1 == count2 ||count1 < count2 )
    	/*{
    	for(i = 0; i < count1; i++)
    		printf(" you entered %c\n", pstr1 + i);*/
    	{
    	printf(" %s %s and \n", pstr1,pstr2);
    			
    	printf(" %s %s\n",strrev(pstr1), pstr2);
    	
    	}
    	else
    	/* concatenate str1 to end of str2 */
    	{
    	printf(" %s %s and \n", pstr2,pstr1);
    
    	printf(" %s %s \n",strrev(pstr2),pstr1);
    	
    	}
    
    	printf(" Would you like to continue? y or n:\n"); /* ask user if they want to enter more data */
    
    	scanf("%c", &more);									/* read the input */
    
    
    	while(getchar() != '\n') ; /* flush input buffer it appears that the scanf leaves a \n inthe buffer */
    	
    	}while(more == 'y' || more == 'Y'); /*Test to continue or not*/
    
    
    	printf("The strings enter were %s\n %s\n", pstr1, pstr2);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main(void)
    Read this

    >>count1 = strlen(pstr1);
    You are counting the length of a string that you haven't yet read in.

    >>flushing input
    Read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Thanks, That is some damn good information. The book I have does not cover these two questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM