how to stop this proccess prematurely..

This is a discussion on how to stop this proccess prematurely.. within the C Programming forums, part of the General Programming Boards category; I think you are simply having a case of 'fence-post-error' aka "off-by-one" error (look either of that up in google). ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you are simply having a case of 'fence-post-error' aka "off-by-one" error (look either of that up in google).

    --
    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.

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you said that its not on the right order
    why??

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    where is the mistake
    why its giving me 0 on a legal input??

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    where is the mistake
    why its giving me 0 on a legal input??
    Have you tried using a debugger?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried debugger
    it stops on the getchar line
    it want me to press a number and enter
    but when i run it normally i inputs a several numbers in a row

    so debugger cant help

    ??

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    it stops on the getchar line
    it want me to press a number and enter
    but when i run it normally i inputs a several numbers in a row
    So input several numbers in a row.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i notices that it doesnt goto the i==size "if"

    why?
    the ch variable during the debbuging proccess gives number like 49
    instead of numbers that i entered
    Last edited by transgalactic2; 01-28-2009 at 11:21 AM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    i notices that it doesnt goto the i==size "if"

    why?
    The problem is that you use getchar() before using scanf(). This means that if there is a single digit number in the input buffer, it would mysteriously "disappear" because it would be read and discarded by the getchar(). A solution is to use scanf() and then use getchar().

    Quote Originally Posted by transgalactic2
    the ch variable during the debbuging proccess gives number like 49
    instead of numbers that i entered
    That's fine. Remember that in ASCII the value of '1' is 49.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did like you said
    when i input
    1 2 3 4 5 6 7 8 enter
    it returns 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))
        {
           return 0;
        }
    
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Suppose when you use scanf(), there is nothing to read. What happens? Suppose when you use getchar(), there is nothing to read. What happens?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in this broken code
    nothing happens

    it doesnt stop the proccess
    it goes to a new line
    till it get a real input

  12. #27
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    where is my mistake
    i flipped the getchar with the scanf in places
    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))
        {
           return 0;
        }
    
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    where is my mistake
    i flipped the getchar with the scanf in places
    That's only part of the fix. As I mentioned, you need to handle EOF correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in the while loop that you showed me you handled EOF by ch!=EOF or
    ch>=0
    i need to use this term here to?
    you mean that
    ??
    Last edited by transgalactic2; 01-28-2009 at 12:42 PM.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    in the while loop that you showed me you handled EOF by ch!=EOF or
    ch>=0
    i need to use this term here to?
    you mean that
    ??
    You DO need to handle EOF at all times when dealing with input.

    However, I think it's not part of the problem you are having right now - your problem is about the order you do things, and how you count them.

    --
    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.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  3. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  4. Proccess and Thread ?
    By Devil Panther in forum Windows Programming
    Replies: 9
    Last Post: 11-17-2003, 11:59 AM
  5. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM

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