Thread: how to stop this proccess prematurely..

  1. #46
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    whats "one off"
    i searched in google there is no definition

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Obviously need to practice your google skills - http://en.wikipedia.org/wiki/Off-by-one_error

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

  3. #48
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    i dont know why
    ??
    When do you expect i to equal size? What does i actually equal at that point?

    Don't answer just one question. Answer both of them, because until you can answer both questions, you will never be able to solve this problem.
    Last edited by MK27; 01-28-2009 at 04:39 PM.
    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

  4. #49
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i debugged it again
    and when i=7 (the last char before enter)
    i goes to

    Code:
    	 if ((ch=='\n')&&(i!=size))
        {
           return 0;
        }
    so i guessed i[7] gets the \n char
    so i changed i==size-1
    now it returns 1 but
    it doesnt put the 8th number in the array
    ??
    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-1)
    	{
    
    	   return 1;
    	}
        
        
         flag=scanf("%d",&input[i]);
    	 ch=getchar();
    	 if ((ch=='\n')&&(i!=size))
        {
           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 02:27 AM.

  5. #50
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what do you think you should do to fix that?

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

  6. #51
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i should some how catch this eights char using getchar
    and put it into the array

    but where to do that?

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i should some how catch this eights char using getchar
    and put it into the array

    but where to do that?
    Think about what happens in your code, and where you get to for each possible scenario of newlines, count of i, and numbers entered. Where should you exit the recursion? What are the conditions?

    Coding is about solving problems - you _NEED_ to learn to think about what happens in your code, not just repeatedly post "and how do I do that?"

    --
    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. #53
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i get \n only onces i the end of the array

    i dont know why it happens

  9. #54
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i get \n only onces i the end of the array

    i dont know why it happens
    Because there is only one newline in the input, perhaps?

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

  10. #55
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thats how it should be
    a row of numbers and enter

    what is the problem?

    one new line means one enter
    Last edited by transgalactic2; 01-29-2009 at 08:38 AM.

  11. #56
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i solved it thanks

  12. #57
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to check if there are more member then the size??

  13. #58
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you count to MORE than size and haven't received a newline yet, is that a good enough clue?

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

  14. #59
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    nothing stops me from counting further
    so its not much an obsticle

  15. #60
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    nothing stops me from counting further
    so its not much an obsticle
    Nothing, except your code, you mean?

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