Thread: how to know if there is some char in the buffer..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to know if there is some char in the buffer..

    if i input 9 chars instead of 8
    it returns 1
    although it returns 0
    how to check for that additional char??

    is this the correct way?
    Code:
    int i;
    i=getchar();
    if (i!=0)
    {
    return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    if i input 9 chars instead of 8
    it returns 1
    although it returns 0
    how to check for that additional char??

    is this the correct way?
    Code:
    int i;
    i=getchar();
    if (i!=0)
    {
    return 0;
    }
    Where do you input these chars?
    Who is "it" that returns 1?
    what do you call additional character?

    You ask your questions as if everybody have read your previous multipost discussion in whole... You are wrong on this assumption.

    your way is incorrect in any way - because if no chars are left getchar will wait till you enter more.
    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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no standard IO function that allows you to check if the input buffer has a character available or not.

    In a Linux OS, I guess you COULD use select() with the filenumber of stdin as argument [but it will stlll not tell you the complete truth, as stdin is buffered, so it may have buffered some of the data and have data ready to read].

    Generally, you have to rely on KNOWING what went on before the getchar() to know if you have more characters to read or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i enter
    1 2 3 4 5 6 7 8 9
    it should return 0

    it doesnt further after 1 2 3 4 5 6 7 8
    if the size is 8
    then we need to have only 8 numbers in a row
    9 numbers in a row is an error

    i tried this
    is it correct?
    Code:
    int i;
    i=getchar();
    if (i!=0)
    {
    return 0;
    }
    Code:
    
    #include <stdio.h>
    int read_array(int input[],int i,int size);
    
    int main()
    {
    	int i;
        int input[40];
        printf("%d\n",read_array(input,0,8));
      for(i=0;i<8;i++)
      {
        printf("%d ",input[i]);
      }
       printf("\n");
       return 0;
    }
    int read_array(int input[],int i,int size)
    {
    	int flag,rt,ch;
     	if (i==size)
    	{
           
    	   return 1;
    	}
        
        
         flag=scanf("%d",&input[i]);
    	 ch=getchar();
    	 if ((ch=='\n')&&(i!=size-1))
        {
           return 0;
        }
    
    	 if (ch<0)
        {
    		return 0;
    	}
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you return 1 when you have collected size numbers, whether or not you have found the newline at the end of the line.

    Again, you need to figure out what the correct order of events is, and how that corresponds to your specifications.

    --
    Mats
    Last edited by matsp; 01-29-2009 at 10:25 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    read the whole line and then use strtol to convert each number from the string, thus you know exactly if something left to convert
    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

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    read the whole line and then use strtol to convert each number from the string, thus you know exactly if something left to convert
    Except if you had read t' other thread, you'd know that no other input functions than scanf(), gets() and getchar() are allowed, and no functions to convert strings.

    There is a relatively simple answer to the problem, but I'd like transgalactic2 to figure out the answer, rather than me spoon feeding it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to put it in a base case
    it returns 0 for nine numbers
    but for the legal input of
    1 2 3 4 5 6 7 8
    it stops after two enters and return 0

    ??
    Code:
    
    #include <stdio.h>
    int read_array(int input[],int i,int size);
    
    int main()
    {
    	int i;
        int input[40];
        printf("%d\n",read_array(input,0,8));
      for(i=0;i<8;i++)
      {
        printf("%d ",input[i]);
      }
       printf("\n");
       return 0;
    }
    int read_array(int input[],int i,int size)
    {
    	int flag,rt,ch;
     	if (i==size)
    	{
           ch=getchar();
           if (ch!=0)
            {
               return 0;
            }
    	   return 1;
    	}
        
        
         flag=scanf("%d",&input[i]);
    	 ch=getchar();
    	 if ((ch=='\n')&&(i!=size-1))
        {
           return 0;
        }
    
    	 if (ch<0)
        {
    		return 0;
    	}
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matsp View Post
    Except if you had read t' other thread, you'd know that no other input functions than scanf(), gets() and getchar() are allowed, and no functions to convert strings.

    There is a relatively simple answer to the problem, but I'd like transgalactic2 to figure out the answer, rather than me spoon feeding it.

    --
    Mats
    if some tutor requires to use gets - it is time to look for another one.

    scanf could read the whole line, you know?

    and convert char to int - could be written manually without any function
    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

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    did i get it close??

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by matsp View Post
    There is no standard IO function that allows you to check if the input buffer has a character available or not.

    In a Linux OS, I guess you COULD use select() with the filenumber of stdin as argument [but it will stlll not tell you the complete truth, as stdin is buffered, so it may have buffered some of the data and have data ready to read].

    Generally, you have to rely on KNOWING what went on before the getchar() to know if you have more characters to read or not.

    --
    Mats
    But the only thing a peek operation would do is return the next character, but leave it in the stream for reading. That's easy enough to write... or my guess is that fgetc was usually used in these situations with ungetc.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i can use only getchar scanf gets
    how to catch this extra char if it exists
    return 0
    if not return 1
    ??

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    and i got another problem that if my input ends with a space
    then it doesnt stops on enter
    but goes to a new line
    Code:
    
    #include <stdio.h>
    int read_array(int input[],int i,int size);
    
    int main()
    {
    	int i;
        int input[40];
        printf("%d\n",read_array(input,0,8));
      for(i=0;i<8;i++)
      {
        printf("%d ",input[i]);
      }
       printf("\n");
       return 0;
    }
    int read_array(int input[],int i,int size)
    {
    	int flag,rt,ch;
     	if (i==size)
    	{
            
            
    	   return 1;
    	}
        
        
         flag=scanf("%d",&input[i]);
    	 ch=getchar();
    	 if ((ch=='\n')&&(i!=size-1))
        {
           return 0;
        }
    
    	 if (ch<0)
        {
    		return 0;
    	}
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }
    Last edited by transgalactic2; 01-29-2009 at 11:55 AM.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    and i got another problem that if my input ends with a space
    then it doesnt stops on enter
    but goes to a new line
    That's because you are doing the same thing as this
    Code:
    #include <stdio.h>
    
    int main() {
    	int ray[4], i=0;
    	printf("Enter: ");
    	while (i<4) {
    		scanf("%d",&ray[i]);
    		i++;
    		if (i<4) if (getchar()=='\n') puts("Not enough numbers yet!");
    	}
    	for (i=0; i<4; i++) printf("%d\n",ray[i]);
    	return 0;
    }
    If you enter "3 45 6SPACE" and then press enter, getchar catches the space and not the newline. But you cannot trap a space, because the spaces are necessary.

    What you want, as I tried to tell you yesterday or the day before, is not possible this way. You must write a function using getchar to process the input one character at a time.

    If you keep trying to do it your way anyway, you will never run out of little problems like this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so you say that
    i need to save it as a string
    and then transform it into an array of integer
    but its a very difficult thing to transform

    first because
    if i read it from left to right
    and i will start "-2345" string
    how could i know to multiply 2 by 10^3 +3*10^2+4*10^1 +5*10^1
    from the starting point i cant know when it ends
    and i got this "-" or "+" char
    and doing that recursively its impossible
    Last edited by transgalactic2; 01-29-2009 at 12:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  4. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM