Thread: Problem in a function

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Problem in a function

    I m trying to code a indention fixer that will first after every 4 loop will add 4 spaces this just the base i m trying to figure out if i found a for then i will read it then jump to next line to add 4 spaces but problem is how can i jump to next line to add the 4 spaces ?? anyways here a code whish i m sure doesnt work Please any ideas how can if i found strsr for for and read it till 0 i wanna jump to next line to add 4 spaces how can i jump to it ? anyways here is current code
    Code:
    int search_it(char *buffer)
    {
         int i;
    	 for(i=0;buffer[i]!=0;i++){
    		 if(buffer[i]=='{')
    			 return 1;
    		 else
    			 return 0;
    	 }
    }
    void canctnateit(char *buffer,char *buffer1)
    {
         strcpy(buffer1,"    ");
         strcat(buffer1,buffer);
         strcpy(buffer,buffer1);
    }
    void indenter_fixer(char *buffer)
    {
         int i,x=0;
    	 char *search;
    	 char buffer1[100];
         for(i=0;buffer[i]!='\0';i++){
    		 if(strstr(buffer,"for")&& search_it(buffer))
    		 {
    			 MessageBox(NULL,"cool we found it","Founder",MB_OK);
    			 x++;
    			 if(x==1){
    				 x=0;
    				 canctnateit(buffer,buffer1);}
    		 }
    	 }
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If "buffer" contains multiple lines it will contain a '\n' between them; if not it is up to you how to read the next line in.
    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
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i tried but program crashed after i puted buffer
    Code:
    void indenter_fixer(char *buffer)
    {
         int i,x;
    	 char *search;
         for(i=0;buffer[i]!='\0';i++){
    		 if(strstr(buffer,"for"))
    			 search_it(buffer);
    	 }
    }
    int search_it(char *buffer)
    {
         int i;
    	 char buffer1[100];
    	 for(i=0;buffer[i]!=0;i++){
    		 if(buffer[i]=='\n'){
    			 canctnateit(buffer,buffer1);
    		 }
    	 }
    	puts(buffer); //this code here make it crash << 
    }
    void canctnateit(char *buffer,char *buffer1)
    {
         strcpy(buffer1,"    ");
         strcat(buffer1,buffer);
         strcpy(buffer,buffer1);
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    this is weird i tried a function it works and doesnt work so weird for example this code
    Code:
    #include <stdio.h>
    #include <string.h>
    #define DEL_IT(P,N)P[N]=0
    void copy_it(char *buffer,char *save_it,int num)//num will be the i
    {
         int i,x;
         for(i=num,x=0;save_it[x]=buffer[i];i++,x++);
    }
    int main(void)
    {
        char name[]="dude\nhell";
        char buffer[100];
        int i;
        for(i=0;name[i]!=0;i++){
            if(name[i]=='\n')
            {
                copy_it(name,buffer,i);
                DEL_IT(name,i);
                strcat(name,"    ");
                strcat(name,buffer);
            }
        }
        puts(name);
        puts(buffer);
        getchar();
        return 0;
    }
    doesnt work
    but this does
    Code:
    #include <stdio.h>
    #include <string.h>
    #define DEL_IT(P,N)P[N]=0
    void copy_it(char *buffer,char *save_it,int num)//num will be the i
    {
         int i,x;
         for(i=num,x=0;save_it[x]=buffer[i];i++,x++);
    }
    int main(void)
    {
        char name[]="dude\nhell";
        char buffer[100];
        int i;
        for(i=0;name[i]!=0;i++){
            if(name[i]=='\n')
            {
                copy_it(name,buffer,i);
                DEL_IT(name,i);
                strcat(name,"    ");
                strcat(name,"HELL");
            }
        }
        puts(name);
        puts(buffer);
        getchar();
        return 0;
    }
    wat the hell is wrong ????

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An incomplete array is defined as having the exact number of members you've given it at the time of declaration. Since this is a character array initialized with a string, it's ending up with exactly 10 characters.

    You are trying to copy 14 characters into it. That's bad.


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

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    so how can i fix in order that i wanna add at the next line of the for loop 4 spaces?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop using an incomplete array, and give it an actual larger size?

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

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    why then when i added HELL it worked? and i did give it a larger size now it did same thing compiled then exited

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You got lucky. Neither one of them should work. I didn't see you adding four spaces in on the second one. But no, neither one of them are correct.


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

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    okay look i added here larger size of the array and it still didnt work i m compiling with gcc
    Code:
    #include <stdio.h>
    #include <string.h>
    #define DEL_IT(P,N)P[N]=0
    void copy_it(char *buffer,char *save_it,int num)//num will be the i
    {
         int i,x;
         for(i=num,x=0;save_it[x]=buffer[i];i++,x++);
    }
    int main(void)
    {
        char name[40]="dude\nhell";
        char buffer[100];
        int i;
        for(i=0;name[i]!=0;i++){
            if(name[i]=='\n')
            {
                copy_it(name,buffer,i);
                DEL_IT(name,i);
                strcat(name,"    ");
                strcat(name,buffer);
            }
        }
        puts(name);
        puts(buffer);
        getchar();
        return 0;
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't skipping the newline. You're copying it into the string over and over again, while you're appending the other words.


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

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh yahhh thanks dude ...would have noticed it should i just increment name[i+1] ?

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    finally fixed it lol
    Code:
    #include <stdio.h>
    #include <string.h>
    #define DEL_IT(P,N)P[N]=0
    void copy_it(char *buffer,char *save_it,int num)//num will be the i
    {
         int i,x;
         for(i=num,x=0;save_it[x]=buffer[i];i++,x++);
    }
    int main(void)
    {
        char name[40]="dude\nhell";
        char buffer[100];
        int i;
        for(i=0;name[i]!=0;i++){
            if(name[i]=='\n')
            {
                i++;
                copy_it(name,buffer,i);
                DEL_IT(name,i);
                strcat(name,"    ");
                strcat(name,buffer);
            }
        }
        puts(name);
        getchar();
        return 0;
    }

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    thanks dude for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM