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 added an "if" command that if ch<0 return 0 but its still doesnt work ?? Code: #include <stdio.h> int ...

  1. #31
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i added an "if" command that if ch<0 return 0
    but its still doesnt work

    ??
    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 (ch<0)
        {
    		return 0;
    	}
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i added an "if" command that if ch<0 return 0
    but its still doesnt work

    ??
    Like I said, yes you do need to deal with EOF at some point - but at this point in time, you should concentrate on getting your logic right. Perhaps adding a printf or two in the function would help - for example printing "i" at the entry of the function, and the number you received from scanf?

    --
    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. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Thinking about the problem further, I think that the scanf() + getchar() combination is problematic. Before we continue, what are your exact and complete requirements?

    Honestly, just write out the exact assignment that you have been given, word for word, with every comma and full stop. You have a history of leaving out important details which in turn frustrates those who suggest things to you that are forbidden, but you conveniently did not say so.
    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

  4. #34
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    my whole program is almost complete i just need to do this array input part
    i need to enter numbers in a row like +001 -007 8 9 6 and the size of the array
    should be 5 for this example to be a legal input
    also it should ignore spaces and tabs.

    i need to do this recursively
    no loops allowed
    i can use only
    scanf getchar gets

    if my size is 6
    and i enter 1 2 3 enter
    then it should stop and return 0

    if my size is 3
    and i enter 1 2 3 4 5 enter
    then it should stop and return 0
    Last edited by transgalactic2; 01-28-2009 at 01:16 PM.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, are you allowed any of these:
    ungetc()
    atoi()/strtol()
    strtok()
    isdigit()/isspace()

    Are you allowed to write other functions.

    I don't think it's possible to solve this case RELIABLY with getchar() and scanf() alone - and gets() is really no help here if you can't use some other function to translate the numeric string into an integer.

    --
    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. #36
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    gets scanf and getchar is the only things allowed

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    gets scanf and getchar is the only things allowed
    We have to assume that the problem is solvable, right? Your teacher seems to be a sadist, but I doubt that it's so sadistic that he/she is giving you tasks that are impossible to solve.

    Let's forget about gets(), as it makes no sense for the purpose here (unless you fancy translating a long string into a set of numbers, which is a possibility, but a rather poor one).

    That leaves getchar() and scanf(). getchar() on it's own is no better - it still leaves you doing the "ascii to integer" translation yourself.

    Calling getchar() after scanf() should be fine, as whatever you enter that is a number will have to end with either a non-numeric character or EOF. In valid input [given the format string you have at the moment], the non-numeric character is a whitespace (space, tab or newline). If the input is invalid, then you may have letters or other symbols there - your description doesn't say what to do about that, but I'd say you should probably ignore that case altogether.

    Now, given the above post-conditions of scanf, we have a pretty easy solution for validating the end of input: use getchar after scanf, if it's a whitespace other than newline, there is more input.

    --
    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. #38
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did call getchar after scanf
    and i checked for EOF
    and its not working

    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 (ch<0)
        {
    		return 0;
    	}
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And you didn't read my post prior to the last one:
    add some tracing to see what your function is actually doing.

    --
    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. #40
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you said that its not in the right order
    but i dont know whats the right order

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    you said that its not in the right order
    but i dont know whats the right order
    Neither do I, unless I spend some time tracing through the exact logic of your code - and that's part of what you need to learn how to do - you can't just post the same non-functioning code over and over again until someone posts the right answer for you.

    I guess it would take about 3 different (correctly placed) printf's added to your code to see what is happening, and understand the difference between what actually happens and what you want to have happen. If in doubt, add a few extra printf's. Make sure you can tell WHICH printf is which one -- so mark each one with a letter (A, B, C, etc) at the start. For example:
    Code:
    printf("A i = %d\n", i);
    ...
    printf("B x = %d\n", x);   // No, you don't use x in your code, this is just an example!
    --
    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.

  12. #42
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did debugging and i can see the values of the variables without
    the printf
    but its doesnt enter
    this part
    Code:
    if (i==size)
    	{
    
    	   return 1;
    	}

  13. #43
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so why not? Is i never equal to size? Why not? [No, I don't know the answer, but I can guarantee that it's not because the compiler is broken].

    --
    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. #44
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont know why
    ??

  15. #45
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i dont know why
    ??
    I understand that, and I'm trying to help you FIND out why. You have to learn to debug your own code - it is a VERY important part of programming. So me telling you what is wrong isn't going to teach you anything useful.

    I still think you have a "one off" problem.

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