Thread: how to stop this proccess prematurely..

  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
    28,413
    Quote Originally Posted by transgalactic2
    where is the mistake
    why its giving me 0 on a legal input??
    Have you tried using a debugger?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 12:21 PM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Suppose when you use scanf(), there is nothing to read. What happens? Suppose when you use getchar(), there is nothing to read. What happens?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 01: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.

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, 12:59 PM
  5. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM