Need help flushing buffer

This is a discussion on Need help flushing buffer within the C Programming forums, part of the General Programming Boards category; I think I am not flushing my buffer correctly. I'm trying to check two things in this function. I can ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    Need help flushing buffer

    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';
    	}
    	
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    	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);  //only outward buffers can be flushed, is the C Standard.
    				i = 0;
    				ch = getchar();
    			}
    		}
    		
    		else //this else goes with the *nearest unelsed if statement*
                    //not the one you have it left justified for.              
    		{
    			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';
    	}
    I'll run it to be sure, but I believe that is your problem.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978
    For reading characters, you're on the right track, but it should be done like this:
    Code:
    int ch;
    while( (ch = getchar()) != EOF && ch != '\n' )
    Your way continues as long as getchar() returns a value that is not zero and not '\n'. While this isn't technically illegal, it's likely not correct. Rather, you tend to want to check for EOF (which cannot be 0) to see if you've encountered end-of-file. For this to properly work, ch needs to be an int, not a char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Flushing buffer
    By $l4xklynx in forum C Programming
    Replies: 6
    Last Post: 03-04-2009, 09:07 PM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21