Thread: Clearing Buffer Problem (beginners Q)

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Clearing Buffer Problem (beginners Q)

    Hello,

    I have run into this problem before but have always been able to sneak around it, but, now I really have to figure out what I'm doing wrong.

    Basically when I enter input that is to big ie...234327432 words, it stays in the buffer, so it always promps the user that their input is to big.

    I thought the command;
    Code:
    while(fgetc(stdin)!='\n');
    addressed that issue?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char size[3];
    	int a;
    	while(1)
    	{
    		A : printf("Please enter how many words you want: ");
    		fflush(stdout);
    		for (a=0; a<3; a++)
    		{
    			size[a]=fgetc(stdin);
    			if (size[0] == '\n')
    			{
    				printf("Input is empty\n");
    				goto A;
    			}
    			if (atoi(size) > 100)
    			{
    				printf("Input is too long\n");
    				while(fgetc(stdin)!='\n');
    				goto A;
    			}
    			if (size[a] == '\n')
    				break;
    
    		}
    
    		break;
    
    
        }
        printf("You want %d words.", atoi(size));
    
        return 0;
    
        }
    Any help would be great thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    This is irrelevant to your question, but why do you have a while(1) loop that breaks on the first iteration? Why have a loop at all? Instead of using goto you could just break out of the for loop instead then.

    Maybe you want to get a string using fgets (see the FAQ) then convert that to an integer? Seems like it would be a whole lot simpler; although I think you would have to chop the \n char off the end of it first.
    Last edited by mike_g; 03-18-2008 at 12:42 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    You aren't passing atoi() a properly null-terminated string.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by arpsmack View Post
    You aren't passing atoi() a properly null-terminated string.
    Meaning?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to add '\0' char at the end of the string so the atoi will know where to stop parsing

    why you do not use fgets?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Iconate View Post
    I thought the command;
    Code:
    while(fgetc(stdin)!='\n');
    addressed that issue?
    This doesn't address your real question, but the line of code listed above is wrong. If you reach EOF before seeing a newline, in other words, if the last line of input has no newline on it, then this loop will spin infinitely. fgetc() will repeatedly return EOF, and EOF is not equal to '\n', so this never breaks. Instead, use:

    Code:
    while((ch = fgetc(stdin)) != EOF && ch != '\n')
        ; /* I usually place empty statements on their own line, to make it explicit that it's not a mistake. */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning addresses, buffer not clearing
    By Iconate in forum C Programming
    Replies: 2
    Last Post: 04-07-2008, 01:18 PM
  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 problem
    By Narcose in forum Game Programming
    Replies: 3
    Last Post: 03-24-2006, 09:29 AM
  4. clearing the buffer - but not the newline
    By Ash1981 in forum C Programming
    Replies: 13
    Last Post: 01-06-2006, 05:22 AM
  5. Popen problem with stdout buffer
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 05-26-2004, 03:00 AM