Thread: code for the correction..

  1. #1
    Registered User mrprogrmr's Avatar
    Join Date
    Dec 2004
    Posts
    9

    code for the correction..

    Hi Frendz,
    I v gotta problem fr u 2 help me.

    i m to read a string from a file. That part z covered but
    then i fail to sort it out..
    its like "add person 3 2003 2004 2005"
    person is a word being checked and if it exists.
    * 3 is the no of persons and can be any number equal or less than 16.
    * 2004 , 2005, 2006 are the person_ids.
    then i want it as
    party[0] ="2004"
    party[1]="2005"
    party[2]="2006"
    since all of them are eqally sized 4
    i am using this in line of code
    ==============================================
    Code:
    if(strstr(buffer,"add person"))
      {
          for(p=11,q=0;buffer[p]!=' ';p++,q++) // No. of persons
            {
               str1[q]=buffer[p];     
               // This wud be the number of persons to include
            }
                        str1[q]='\0';
           char *strs;
         strcpy(strs,str1);
                      num=atoi(strs);
         printf("Num::%d",num);
         nos=num;
         t=11+strlen(strs);
         while(nos>0) 
    // this is the number of numbers reducing with each char string
         {
             static int t;
                     char *chid;
             x=0; 
             
             for(i=t,p=0;buffer[i]!=' '&& p<4;i++,p++)
                 {
              ch_id[p]=buffer[i];
                                        t=i;
                     }
                ch_id[p]='\0';
              // printf(" %d ). %x\n" ,x,ch_id);
              ch_ids[x]=chid;
              x+=1;
              nos-=1;
              free(chid);                                   
           }
    
         //The rest part..
    }
    hope u diagnosed my fault which took enough of my mind and patience.
    looking for your kind reply..


    amit ranjan mish

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Overheard: "Somewhere, Mr. Webster cries softly."

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    * S T U D E N T *
    Join Date
    Oct 2003
    Posts
    30
    what sort of errors are you getting ?

    im not good at C but i know that meaningful variable names would help a lot. what the hell is 'p', 'q' etc etc

    --CHris :: g00$em@tt
    'rm -fr /bin/laden'
    'kill all'

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, right. Helpful. Consider using strtok to break your string up into segments. Then use something like atoi to turn strings of numeric characters into integers. Much cleaner.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char *strs;
    > strcpy(strs,str1);
    > num=atoi(strs);
    1. What did you indent this code with?
    2. C doesn't (yet) have declarations in the body of the code, so char *strs; should be elsewhere. So you're either using C++ or some non-standard (or uncommon C99) compiler.
    3. strs isn't allocated, so your strcpy is just blasting data who knows where.
    4. You don't need to strcpy it anyway just to use atoi()
    5. You don't need any of that "copy to a temporary buffer" if you use strtol()

  6. #6
    Registered User mrprogrmr's Avatar
    Join Date
    Dec 2004
    Posts
    9
    // apologise for the mysterious words.. but some of the but p,q have good reasons to be
    // their in the original application.
    Code:
    /* The following segment looks for the string starting with ‘add person’ .
         IF FOUND, it would check for  the number of persons in the party. 
         p, q refers to the character count, starting from the 11th count it reads the characters  	and puts them in a character string str1[] unless a blank space is found . */
    static int t;
    char *chid;
    char ch_id[4];
    char *ch_ids[4];
    
    if(strstr(buffer,"add person"))
      {
          for(p=11,q=0;buffer[p]!=' ';p++,q++) // No. of persons
            {
               str1[q]=buffer[p];     
    
            }
                        str1[q]='\0';
    
         num=atoi(str1);
         printf("Num::%d",num);
         nos=num;
         t=11+strlen(str1);
    /* nos refers to number of numbers . Say for three persons it would create room for  an  	array of 3 strings. */ 
         while(nos>0) 
    // this is the number of numbers reducing with each char string
         {
             x=0; 
             for( i = t+1, p = 0; buffer[i] ! = ' ' && p<4 ; i++, p++)
                 {
              		ch_id[p]=buffer[i];
                            t=i;
                     }
                ch_id[p]='\0';
              // printf(" %d ). %x\n" ,x,ch_id);
                 strcpy(chid,ch_id);
                 ch_ids[x]=chid;
                 x+=1;
              nos-=1;
              free(chid);                                   
           }
    
         //The rest part of the application goes here.
    }
    
    The desired output should be
    Command 	:  “ add friends 3 2005 2007 2009”
    Output[1]	:     2005
    Output[1]	:     2007
    Output[1]	:     2009
    Last edited by Salem; 12-15-2004 at 04:28 AM. Reason: Added code tags - next time, do it yourself

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They weren't questioning the location of the variables p and q, rather, the names:
    // apologise for the mysterious words.. but some of the but p,q have good reasons to be
    // their in the original application.
    /* The following segment looks for the string starting with ‘add person’ .
    IF FOUND, it would check for the number of persons in the party.
    p, q refers to the character count, starting from the 11th count it reads the characters and puts them in a character string str1[] unless a blank space is found . */
    I imagine they had better names for the variables in mind when they replied. However, I still feel you'd still be better served using something like strtok and atoi to do all the messy work for you here.

    To each his own as they say.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User mrprogrmr's Avatar
    Join Date
    Dec 2004
    Posts
    9
    .......but would it keep count of the no. of persons in party..this no. could be anything between 3 to 16 and person id needs to be kept along.
    problem lies here
    Code:
         while(nos>0) // this is the number of numbers reducing with each char string
    					{
    						char *chid;
             			
    						x=0; 
    						
    				for(i=t+1,p=0;buffer[i]!=' '&& p<4;i++,p++)
    						{
    							ch_id[p]=buffer[i];
    							t=i;	
         					}
    						
       						ch_id[p]='\0';
    						printf(" %d ). %s\n" ,x,ch_id);
    					//	strcpy(chid,ch_id);
    						ch_ids[x]=ch_id;
    						x+=1;
    						nos-=1;
    				-->	free(ch_id);							
    					 }




    dear quzah..

    To sum it all I show u the algo that would clear all that it means to me..

    Code:
    
    IF FOUND “ add person”
         Evaluate the number of person to add to the party
                 While the number of person is greater than 0
                            Read the person id 
                            Store into an array
                            Decrement counter of persons 
                 Wend
    Endif
    Last edited by mrprogrmr; 12-15-2004 at 04:53 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main ( ) {
      char test[] = "add person 3 2003 2004 2005";
    
      if ( strstr(test,"add person") != NULL ) {
        char *p, *endp;
        p = &test[11];  /* skip the words */
    
        /* walk along the string converting values */
        while ( p != NULL && *p != '\0' ) {
          long int result;
    
          /* convert a number - check errno to be really safe */
          result = strtol( p, &endp, 10 );
          printf( "Found %li\n", result );
    
          /* endp is where we got to, so start here for the next number */
          p = endp;
        }
      }
    
      getchar();
      return 0;
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since you apparently don't understand what I was trying to say:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main( void )
    {
            char *tok, *start;
            char foo[] = "add person 4 1234 2345 3456 4567";
    
            start = strstr( foo, "add person" );
            if( start )
            {
                    start += strlen( "add person" );
                    tok = strtok( start, " " );
                    while( tok != NULL )
                    {
                            printf("\"%s\", is %d\n", tok, atoi( tok ) );
                            tok = strtok( NULL, " " );
                    }
            }
            return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User mrprogrmr's Avatar
    Join Date
    Dec 2004
    Posts
    9
    dear quzah,
    Thnx for the solution and the effort put in by you to solve the problem.
    I too agree that the solution given by you is better compared to mine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM