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 need to enter numbers in a row of certain length. if i deside size =3 then i have to ...

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

    how to stop this proccess prematurely..

    i need to enter numbers in a row
    of certain length.
    if i deside size =3
    then i have to input 3 2 7
    if i press enter in the middle of the proccess it doesnt stop'
    it just enters a new line and continues from there
    i want it to sent an error instead.
    so if i will press 3 2 enter
    it will return an error
    i tried to do it here // in the commented line "\n line"
    its now 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");
    }
    int read_array(int input[],int i,int size)
    {
    	int flag,rt;
           
    	if (i==size)
    	{
         
    	   return 1;
    	}
         flag=scanf("%d",&input[i]);  "\n line"
    	 if (input[i]=='\n')
    	 {
            return 0;
    	 }
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size); 
    	 return rt;
    }
    Last edited by transgalactic2; 01-28-2009 at 06:52 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Again, transgalactic2, you will be much better off if you write a function to get a line of input from a stream, a reusable function that will work with files and stdin (the keyboard). You only have to write the function once and you can use it in everything you write where you are trying to get a line of input. Then you can sscanf the line if you want or modify the function.

    Instead, you waste more time trying to make scanf work in ways that it will not.
    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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	 if (input[i]=='\n')
    	 {
            return 0;
    	 }
    This if-statement will be true when you enter 10 as the number, not when you hit enter.

    There is no reasonable way that you can read a number from input with scanf() and see if "nothing was entered, just a newline", since part of scanf is to ignore newlines, spaces, tabs, etc from the 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.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so how to change it so it
    wont ignore enter??

    i tried to add another base case
    but its not working
    i cant use sscanf
    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");
    }
    int read_array(int input[],int i,int size)
    {
    	int flag,rt;
           
    	 if (input[i]=='\n')
    	 {
            return 0;
    	 }
    	if (i==size)
    	{
         
    	   return 1;
    	}
         flag=scanf("%d",&input[i]);  
    	
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size); 
    	 return rt;
    }
    Last edited by transgalactic2; 01-28-2009 at 06:52 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    scanf() [when using numeric input formats, such as %d, %x, %i, %f, %lf, %u, etc] will ALWAYS ignore enter. The only option, if you want to know if the following digit is a newline or not is to read a string first, then use sscanf() and other string operations to determine the amount of input actually there.

    --
    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. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use sscanf
    how to catch the char before it goes to the scanf

    if i would have that
    then i could stop the function and return 0
    ??

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can i use getchar() ??
    i saw in the loop
    stuff like
    x=getchar() !='\0'

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    can i use getchar() ??
    i saw in the loop
    stuff like
    x=getchar() !='\0'
    If you read a number at a time, you could use getchar() after the scanf(), since you are guaranteed that scanf will not until there is something terminating the numeric input.

    However, if the input is absolutely empty, you don't get out of scanf until a number has been entered.

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

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what do you mean by 1 number at a time


    i want to read nuumbers in a row

    if size is 5
    1 2 3 4 5 enter it will be return 1
    if i will enter
    1 2 3 enter it will return 0

    i could do it by how i described with getchar() before??

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you check if getchar() gives you a space or a newline after reading a number - if it's a newline, then that was the end of that line.

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

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i wrote this code
    it does stops prematurely
    when i input
    1 2 3 4 5 enter it stops and return 0 ( )

    but on the other hand
    when i input
    1 2 3 4 5 6 7 8 enter
    it returns zero too
    why??
    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;
        ch=getchar();
        if ((ch=='\n')&&(i!=size))
        {
           return 0;
        }
    	if (i==size)
    	{
    
    	   return 1;
    	}
         flag=scanf("%d",&input[i]);
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }
    Last edited by transgalactic2; 01-28-2009 at 09:07 AM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Think about the order of things you do in your code.

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

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i flipped the order of the base "if" conditions
    still 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;
    	}
        ch=getchar();
        if ((ch=='\n')&&(i!=size))
        {
           return 0;
        }
    
         flag=scanf("%d",&input[i]);
    
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size);
    	 return rt;
    }

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you enter 9 elements?

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

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    if i enter 9
    it should return 0

    but it returns 1
    its because it had 8 members and it didnt go further
    so what should i do??

Page 1 of 4 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