Thread: why this valud function doesnt work..

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

    why this valud function doesnt work..

    i want to check if there is a char which differs space tab + - or number
    i wrote this long if inside
    i cant see why this if doesnt work
    ??
    Code:
    #include <stdio.h>
    int valid(char buf[],int index);
    int nex_num(char buf[],int index);
    int rid_next(char buf[],int index,int x);
    void spin(char buf[],int arr[],int index,int num);
    int one_num(char buf[],int index,int start);
    int rid(char buf[],int index,int x);
    int main()
    {
    
    	int arr[4],t;
        char buf[] ="-000123		-143	23  +567";
        t=valid(buf,0);
        printf("%d",t);
    return 0;
    }
    
    
    int valid(char buf[],int index)
    {
        if ((buf[index]=='\0')&&(index==0))
        {
           return 0;
        }
        if (buf[index]=='\0')
        {
           return 1;
        }
        if ((buf[index]!='-')||(buf[index]!='+')||(buf[index]!=' ')||(buf[index]!='\t')||((buf[index]<'0')||(buf[index]>'9')))
         {
            return 0;
         }
         return valid(buf,index+1);
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Take this bit:

    Code:
    (buf[index]!='-')||(buf[index]!='+')||(buf[index]!=' ')||(buf[index]!='\t')||((buf[index]<'0')||(buf[index]>'9'))
    What if buf[index] equals '-'?

    That would evaluate to:
    Code:
    (false||true||true||true||true||true)
    Which would evaluate to:
    Code:
    true
    Not what you expected, is it? Look at that bit again, see if you can find out what you did wrong .

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed it to &&
    but
    now it returns 1 for an illegal input

    Code:
    #include <stdio.h>
    int valid(char buf[],int index);
    int nex_num(char buf[],int index);
    int rid_next(char buf[],int index,int x);
    void spin(char buf[],int arr[],int index,int num);
    int one_num(char buf[],int index,int start);
    int rid(char buf[],int index,int x);
    int main()
    {
    
    	int arr[4],t;
        char buf[] ="-000123	p	-143	23  +567";
        t=valid(buf,0);
        printf("%d",t);
    return 0;
    }
    
    
    int valid(char buf[],int index)
    {
        if ((buf[index]=='\0')&&(index==0))
        {
           return 0;
        }
        if (buf[index]=='\0')
        {
           return 1;
        }
        if ((buf[index]!='-')&&(buf[index]!='+')&&(buf[index]!=' ')&&(buf[index]!='\t')&&((buf[index]<'0')&&(buf[index]>'9')))
         {
            return 0;
         }
         return valid(buf,index+1);
    }

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I don't want to be boring, but according to your recent threads, you should really take more time to debug your program, I got this impression you code something, if it doesn't work:
    1. you try once to fix it
    2. if it still doesn't work you post here and wait for suggestions
    3. goto 1
    Plus each time, there is only a small variation in the kind of problem you encounter, so I really think you are able to fix it by yourself if you try harder... this is the right time to improve your debugging strategies...

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    this is the right time to improve your debugging strategies
    One thing I started doing recently, because I am of the opinion that IDE's and GUI debuggers may not be so beneficial to the fledgling programmer, is added a simple macro in my text editor to give me this whenever I press F1:
    Code:
    printf("\n"); fflush(stdout);
    And then fill in the blanks. It's unindented, so easy to find and remove. I know you can already check or see the value of various variables in your GUI IDE/Debugger, but I think it might provide a superior focus to just use a text editor, a console, and some printf statements.

    ps. transgalactic2: the example I gave you in the other thread CAN be adapted to do what you want. The first trick was to remove malloc, the next one to make it return 0 on an error. I also removed a line so that it will process as many numbers as you enter, but only return more than 0 if there was 4. Then I added a line so that it will accept a space after the last number:
    Code:
    #include <string.h>
    #include <stdio.h>
    
    int getintray (char stringray[4][256], int len, int count, const int size) {
    	int ch=getchar();
    //printf("%c count=%d len=%d\n",ch,count,len); fflush(stdout);
    	if (ch==' ') {
    /*		if ((len>0) && (count==size-1)) return len;  REMOVED */
    		if (len>0) stringray[count++][len]='\0'; 
    		return getintray(stringray,0,count,size);
    	} else if (ch=='\n') {
    		if (len>0) count++;     /* new line */ 
    		if (count==size) return 1;
    		else return 0; 
    	} else if ((ch>=48) && (ch<=57)) {
    		stringray[count][len++]=ch;
    		return getintray(stringray,len,count,size);
    	}
    	else return 0;
    } 
    	
    
    int main() {
    	int len, i;
    	char stringray[4][256];
    	printf("Enter 4 numbers seperated by spaces: ");
    	if (getintray(stringray,0,0,4)==0) puts("RETURNED ZERO");   /* when it returns 0 */
    	else for (i=0; i<4; i++) {	/* a loop for the demo */
    		printf("%d: \"%s\"\n",i,stringray[i]);
    	}
    	return 0;
    }
    Some output:
    [root~/C] ./a.out
    Enter 4 numbers seperated by spaces: 123 222 4 9
    0: "123"
    1: "222"
    2: "4"
    3: "9"
    [root~/C] ./a.out
    Enter 4 numbers seperated by spaces: 12 33
    RETURNED ZERO
    [root~/C] ./a.out
    Enter 4 numbers seperated by spaces: asdf 33 2
    RETURNED ZERO
    [root~/C] ./a.out
    Enter 4 numbers seperated by spaces: 12 33 44 3 44 2
    RETURNED ZERO
    [root~/C] ./a.out
    Enter 4 numbers seperated by spaces: 1 2 3 4
    0: "1"
    1: "2"
    2: "3"
    3: "4"

    We all know computer programming is often hard, but don't give up too easily...
    Last edited by MK27; 01-31-2009 at 01:24 PM. Reason: laserlight!
    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

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this big "if" statement should
    work
    i cant debug this single line to see why its not working
    ??
    Last edited by transgalactic2; 01-31-2009 at 01:07 PM.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    if ((buf[index]!='-')&&(buf[index]!='+')&&(buf[index]!=' ')&&(buf[index]!='\t')&&((buf[index]<'0')&&(buf[index]>'9')))
         {
            return 0;
         }
    there are "and" statements all the way in this "if"
    and every one of them should be true on the letter 'p'
    but when it reaches the letter 'p' it doesnt enter the "if"

    why??

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    fixed it

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got another if statement which i cant understand why its not working?
    i got + in the middle of the string alone
    this if:
    Code:
    		if((buf[index+1]==' ')||((buf[index-1]>='0')&&(buf[index-1]<='9')))
    		{
                return 0;
    		}
    should return 0 for it.
    but in the debugger i see that it doesnt go in
    why??
    Code:
    #include <stdio.h>
    int valid_s(char buf[],int index);
    int valid(char buf[],int index);
    int nex_num(char buf[],int index);
    int rid_next(char buf[],int index,int x);
    void spin(char buf[],int arr[],int index,int num);
    int one_num(char buf[],int index,int start);
    int rid(char buf[],int index,int x);
    int main()
    {
    
    	int arr[4],t;
        char buf[] ="-000123	+	-143	23  +567  ";
        //t=valid(buf,0);
    	t=valid_s(buf,0);
        printf("%d",t);
    return 0;
    }
    
    
    int valid(char buf[],int index)
    {
        if ((buf[index]=='\0')&&(index==0))
        {
           return 0;
        }
        if (buf[index]=='\0')
        {
           return 1;
        }
        if ((buf[index]!='-')&&(buf[index]!='+')&&(buf[index]!=' ')&&(buf[index]!='\t')&&((buf[index]<'0')||(buf[index]>'9')))
         {
            return 0;
         }
         return valid(buf,index+1);
    }
    int valid_s(char buf[],int index)
    {
    	if ((buf[index]=='\0')&&(index==0))
        {
           return 0;
        }
    	if (buf[index]=='\0')
    	{
           return 1;
    	}
        if ((buf[index]=='+')||(buf[index]=='-'))
        {
          
    		if((buf[index+1]==' ')||((buf[index-1]>='0')&&(buf[index-1]<='9')))
    		{
                return 0;
    		}
    		if((buf[index+1]=='-')||(buf[index+1]=='+'))
    		{
               return 0;
    		}
        }
    	return valid_s(buf,index+1);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM