Whats Wrong with these 2 IFs???! Is it char comparison?

This is a discussion on Whats Wrong with these 2 IFs???! Is it char comparison? within the C Programming forums, part of the General Programming Boards category; Greetings from Greece to the community Im making a console programm which finds the asked words into the cryptolex by ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Angry Whats Wrong with these 2 IFs???! Is it char comparison?

    Greetings from Greece to the community

    Im making a console programm which finds the asked words into the cryptolex by searching horizontaly

    But when i wrote the Search algorithm, the exe application (console) crashes despite the compiler returns no errors. Its made with dynamic 2D and 1D char Arrays containing single char at each box, and the search algorithm contains a "complex" checking of values with AnchorI and J and 2ond i j

    Its a while with 2 encapsulated whiles and 2 ifs in the 3rd while.

    I get no crash if i put in comments the content of the 2 if in the algorithm. even if set out of comment a single "i=500;" i get crash.
    The 2 if compare the 2 character Arrays. Has this anything to do?

    I dont think so because i've tried a similar comparison in the end of the programm and it was sucesfull with no crash


    I have to make it with dynamical arrays by force (its an exercise in C at my IUT)


    Here is the Algorithm along with the declarations

    PLEASE IF ANYONE CAN TELL ME WHATS WRONG?! (( Cause of the NO ERROR Crash i cant proceed nor check it someway. I thought it was wrong use of the malloc (put int instead of char) or the comparison of char arrays but its not again. (((((((((((((((((( And at this point i cant even test if it does the job of search fine : (((((((((

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<conio.h>
    #include <string.h>
    
    (...)
    
    char** Cryptolexo;   
        int numI=0, numJ=0, iWord=1, NWords=0;
        int x=0, y=0;
        char** FindedWordArray;    
        char *CurentWordArray;
        char *mystring;
        mystring=(char*) malloc (100* sizeof(int));
        CurentWordArray=(char*) malloc (50* sizeof(int));
        
        int CurentWordSize=0, NLettersFound=0;    
         //CurentWordArray=(char*) malloc (CurentWordSize* sizeof(int));
       int Choice=0, i=0,j=0,Tempi=0,Tempj=0, Ianc=0,Janc=0; //Ianchor,Janchor
       int SsameLetters=0;
       FILE * pFile;
       char *ptr = 0;
    
    (...)
    //Reading of Dimensions and creation of 2D dynamical Array
       j=0; i=0;
       while (  (fgets (mystring , 100 , pFile) != NULL) && i<1 ) 
        {    
            numJ = atoi (mystring);
            //printf("\n The Converted String %c in Number is : %d ",numJ, numJ);
            Cryptolexo = (char **) malloc(numJ * sizeof(int *));
            FindedWordArray = (char **) malloc(numJ * sizeof(int *));
    
            numI = atoi (mystring);
                for(x = 0; x < numJ; x++)
                {
                Cryptolexo[x] = (char *) malloc(numI * sizeof(int));
                FindedWordArray[x] = (char *) malloc(numI * sizeof(int));
                }   
            i++;
        }
    (.................)
    //ALGORITHM OF SEARCH
            Ianc=0; Janc=0; j=0; SsameLetters=0;
            while (Ianc<=numI)
              {
                Janc=0;
                while (Janc<=numJ-CurentWordSize)
                {
                  j=0;
                  while (j<=CurentWordSize-1)
                    {
                    //THESE ARE THE IFS TRY PUTING OUT 
                    //OF COMMENTS EVEN THE i=65000; IT WILL CRASH
                    //At the first difference it "kils" the whiles and zeros the 
                    //finded word array to search the next block of  boxes
                      if (Cryptolexo[Ianc][Janc+j]!=CurentWordArray[j])
                        { 
                        /*
                          j=65000;
                        
                          // 0 Zeroing 
                          Tempi=0; Tempj=0;   for(Tempi = 0; Tempi < numI; Tempi++)
                          {for(Tempj = 0; Tempj < numJ; Tempj++){FindedWordArray[Tempi][Tempj]=0;}  }
                          //------------------------------------------
                          */
                        }
                      //if the same sums the same letters
                      if (Cryptolexo[Ianc][Janc+j]==CurentWordArray[j])
                        {/*
                          // printf("Equal");
                           //SsameLetters++;
                           //FindedWordArray[Ianc][Janc+j]=Cryptolexo[Ianc][Janc+j];
                           */
                        }
                      j++;
                    }
                  Janc++;
                  if (SsameLetters==CurentWordSize)
                    { Ianc=65000; Janc=65000; j=65000;
                      // ^^ Write the Finded word Array (cause it has to appera as found)
                      Tempi=0; Tempj=0;   for(Tempi = 0; Tempi < numI; Tempi++)
                      {  for(Tempj = 0; Tempj < numJ; Tempj++){printf("%c", FindedWordArray[Tempi][Tempj]);}printf("\n");  }
                      //-----------------------------------------
                    }
                }
                Ianc++;
              }
              //END-ALGORITHM SEARCH     
            //*/
    Here is the sample similar comparison that works
    Code:
    //TEST OF A SIMILAR COMPARISON THAT WORKS WITH NO PROBLEM
       i=0;j=1; Tempi=1; 
       CurentWordArray[1]='3';
       Cryptolexo[1][1]='3';
       printf("\n\nCurentWord Array[%d]: %c",i, CurentWordArray[i+Tempi]);
       printf("\n\nCurentCryptolexo[%d]: %c",i, Cryptolexo[i+Tempi][1]);
       if(CurentWordArray[i+Tempi]==Cryptolexo[i+Tempi][1]){ printf("\nEqual"); }
       if(CurentWordArray[i+Tempi]!=Cryptolexo[i+Tempi][1]){ printf("\nUnEqual"); }
    Last edited by imakaia; 05-09-2010 at 02:49 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Here is the entire code for refference. Also the contents of the Crypto.exe

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<conio.h>
    #include <string.h>
    
    int main()
    {
        
       /*? is this used? Delete it if not*/ int CurentFileLine=1;
        char** Cryptolexo;   
        int numI=0, numJ=0, iWord=1, NWords=0;
        int x=0, y=0;
        char** FindedWordArray;    
        char *CurentWordArray;
        char *mystring;
        mystring=(char*) malloc (100* sizeof(int));
        CurentWordArray=(char*) malloc (50* sizeof(int));
        
        int CurentWordSize=0, NLettersFound=0;    
         //CurentWordArray=(char*) malloc (CurentWordSize* sizeof(int));
       int Choice=0, i=0,j=0,Tempi=0,Tempj=0, Ianc=0,Janc=0; //Ianchor,Janchor
       int SsameLetters=0;
       FILE * pFile;
       char *ptr = 0;
       
    //Zeroing Array String   
       for (i=0; i<=100; i++ )
        {
            mystring[i]=0;     
        }
    //Open File
        pFile = fopen ("crypto.txt" , "r");
       
    //Reading of Dimensions and creation of 2D dynamical Array
       j=0; i=0;
       while (  (fgets (mystring , 100 , pFile) != NULL) && i<1 ) 
        {    
            numJ = atoi (mystring);
            //printf("\n The Converted String %c in Number is : %d ",numJ, numJ);
            Cryptolexo = (char **) malloc(numJ * sizeof(int *));
            FindedWordArray = (char **) malloc(numJ * sizeof(int *));
    
            numI = atoi (mystring);
                for(x = 0; x < numJ; x++)
                {
                Cryptolexo[x] = (char *) malloc(numI * sizeof(int));
                FindedWordArray[x] = (char *) malloc(numI * sizeof(int));
                }   
            i++;
        }
          
          
    //Reading of Array as its red from the file
        j=0; i=0;   
        while (  (fgets (mystring , 100 , pFile) != NULL) && i<numI ) 
         {
            for(j = 0; j < numJ; j++)
            {
                    Cryptolexo[i][j] = mystring[j];
                    //printf("%c", Cryptolexo[i][j]);       
            }
            //printf("\n");
            i++;          
         }
    
    
    //Shows the Array after it has red it    
        j=0; i=0;
        for(i = 0; i < numI; i++)
        {
            for(j = 0; j < numJ; j++)
            {
                    printf("%c", Cryptolexo[i][j]);
            }
            printf("\n");
        }
        
    //Read of Number of Words to be find into the cryptolex
        NWords = atoi (mystring);
       
       
    //Reading of Each Word and Search
    
       
       
    //Test Read and search of 1 Word "AX" horizontaly in the middle of the array
       printf("\n");
       j=0; i=0;
       while (  /*(fgets (mystring , 100 , pFile) != NULL) &&*/ iWord<=NWords )
         {
            printf("\n\n----------------------------------------\n");
            //Obdainance (from the file) of the Specific word  to be searched and search of it
            //----------------------------------------------------------------  
            // 0 Zeroing     
            for (i=0; i<=100; i++ ){mystring[i]=0;}
            // 0 Zeroing 
            i=0;   while (i<=50 ){CurentWordArray[i]=0;  i++; }
            // 0 Zeroing 
            i=0; j=0;   for(i = 0; i < numI; i++)
                       {  for(j = 0; j < numJ; j++){FindedWordArray[i][j]=0;}  }                     
            //----------------------------------------------------------------
            
            //Read of Word and put letter by letter into the array
            fgets (CurentWordArray , 50 , pFile);
            puts(CurentWordArray);
            //Finds number of letters of the specific word
            i=0; CurentWordSize=0;
            while (CurentWordArray[i]!=0 )
              {
                CurentWordSize++;
                i++; 
                if (CurentWordArray[i]==0){CurentWordSize--;}
              }
            printf("The Size of this Seeking Word is: %d",CurentWordSize);
            //
            //ALGORITHM OF SEARCH
            Ianc=0; Janc=0; j=0; SsameLetters=0;
            while (Ianc<=numI)
              {
                Janc=0;
                while (Janc<=numJ-CurentWordSize)
                {
                  j=0;
                  while (j<=CurentWordSize-1)
                    {
                    //THESE ARE THE IFS TRY PUTING OUT 
                    //OF COMMENTS EVEN THE i=65000; IT WILL CRASH
                    //At the first difference it "kils" the whiles and zeros the 
                    //finded word array to search the next block of  boxes
                      if (Cryptolexo[Ianc][Janc+j]!=CurentWordArray[j])
                        { 
                        /*
                          j=65000;
                        
                          // 0 Zeroing 
                          Tempi=0; Tempj=0;   for(Tempi = 0; Tempi < numI; Tempi++)
                          {for(Tempj = 0; Tempj < numJ; Tempj++){FindedWordArray[Tempi][Tempj]=0;}  }
                          //------------------------------------------
                          */
                        }
                      //if the same sums the same letters
                      if (Cryptolexo[Ianc][Janc+j]==CurentWordArray[j])
                        {/*
                          // printf("Equal");
                           //SsameLetters++;
                           //FindedWordArray[Ianc][Janc+j]=Cryptolexo[Ianc][Janc+j];
                           */
                        }
                      j++;
                    }
                  Janc++;
                  if (SsameLetters==CurentWordSize)
                    { Ianc=65000; Janc=65000; j=65000;
                      // ^^ Write the Finded word Array (cause it has to appera as found)
                      Tempi=0; Tempj=0;   for(Tempi = 0; Tempi < numI; Tempi++)
                      {  for(Tempj = 0; Tempj < numJ; Tempj++){printf("%c", FindedWordArray[Tempi][Tempj]);}printf("\n");  }
                      //-----------------------------------------
                    }
                }
                Ianc++;
              }
              //END-ALGORITHM SEARCH     
            //*/
            
            // printf("\n");
            iWord++;     
         } 
    
       
       
      
       fclose (pFile);
       
       //TEST OF A SIMILAR COMPARISON THAT WORKS WITH NO PROBLEM
       i=0;j=1; Tempi=1; 
       CurentWordArray[1]='3';
       Cryptolexo[1][1]='3';
       printf("\n\nCurentWord Array[%d]: %c",i, CurentWordArray[i+Tempi]);
       printf("\n\nCurentCryptolexo[%d]: %c",i, Cryptolexo[i+Tempi][1]);
       if(CurentWordArray[i+Tempi]==Cryptolexo[i+Tempi][1]){ printf("\nEqual"); }
       if(CurentWordArray[i+Tempi]!=Cryptolexo[i+Tempi][1]){ printf("\nUnEqual"); }
       
       
       scanf("%d", &Choice);
       for(j = 0; j < numJ; j++) {free(Cryptolexo[j]); free(FindedWordArray[j]); }
       free(Cryptolexo);
       free(FindedWordArray);
       return 0;
    }
    THE CONTENTS OF THE Crypto.exe
    The dimensions that the array will have
    The array's contents
    The number of letters contained
    and the letters to be find

    Code:
    5
    5
    OREAO
    LEGT 
    AIIE 
     AXM 
    NSKOE
    9
    AXM
    OREA
    EOK
    OLA
    META
    EIMAI
    OXI
    NAI
    OTI
    Last edited by imakaia; 05-09-2010 at 02:44 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    First, letting Ianc equal the # of elements is an immediate overrun.

    If you run the program in a debugger it will tell you:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x004017bd in main () at <filename>:142
    142                       if (Cryptolexo[Ianc][Janc+j]==CurentWordArray[j])
    (gdb) print Janc
    $1 = 0
    (gdb) print Ianc
    $2 = 0
    (gdb) print j
    $3 = 65000
    Now, you allocate room for, what, four hundred strings? So trying to access string number 65000 is not going to work.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    But even if instead of 65000 put 6 its the same

    after all if you look the 3rd if there the 65000 is allocated with no prob.

    What is going oooon? :'((((((((

    EDIT:

    OOOPS! It was straight that PLUS that i was checking in coordinates out of the array!


    THAAAAAAAAAAAAAAAAAANK YOUUUUUUUUUUUUUU

    YOU SAVED MEEEEEEEEEE!!!!!

    If i dont give this exercise i dont have the wright to give exams in C lab : ))
    Last edited by imakaia; 05-10-2010 at 01:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21