Thread: 6x6 array problem

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    5

    6x6 array problem

    Hi there! I want to do an 6x6 array which contains the english abc + numbers 0-9 (sum: 36 chars). This will be a polybius cipher. I read words from a txt file (for example apple11) and write its number to a txt file (apple11 = 11 34 34 26 15 54 54). It work, but got wrong results. Thanks

    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    int main(void){
    
    
    int i,j,k,x;
    int tomb[36];
    
    
        /*char str[500];
        gets(str);*/
    
    
        FILE* fp = fopen ("be.txt", "r");
        char str[500];
        char* filename = "be.txt";
        fgets(str, 500, fp); // ez lesz a fájlból beolvasás
    
    
     x=0;
     k=11;
    
    
    for(i=0;i<=36;i++)
    
    
    {
     if(i<=6)
     {
          if(i%6==0&&i!=0)
       {
      k+=1;
      tomb[x]=k++;
    
    
        }
       else{
      tomb[x]=k++;
    
    
          }
     }
     if(i>=6)
     {
          if(i%6==0&&i!=0)
       {
    
    
      tomb[x]=k++;
       k+=1;
    
    
        }
       else{
      tomb[x]=k++;
    
    
          }
     }
     if(i==35)
     {
      tomb[x]=36;
     }
      x++;
    }
    
    
    FILE*  outfile = fopen("ki.txt", "w");
    
    
    i=0;
    while(str[i]!='\0')
      {
     if(!((str[i]>=0&&str[i]<=31)||(str[i]>=33&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
     {
       if(str[i]>='A'&&str[i]<='Z')
       {
        fprintf(outfile,"%d\n ",tomb[str[i]-'A']);
       }
         if(str[i]>='a'&&str[i]<='z')
         {
          fprintf(outfile,"%d\n ",tomb[str[i]-'a']);
         }
    
    
     }
    
    
        if(str[i]==' ')
        {
          fprintf(outfile,"%c\n",str[i]);
        }
    
    
       i++;
      }
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    My first thought is, indent your code properly.
    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    int main(void)
    {
      int i, j, k, x;
      int tomb[36];
    
      /*char str[500];
         gets(str); */
      FILE *fp = fopen("be.txt", "r");
      char str[500];
      char *filename = "be.txt";
      fgets(str, 500, fp);          // ez lesz a fájlból beolvasás
    
      x = 0;
      k = 11;
      for (i = 0; i <= 36; i++)
      {
        if (i <= 6) {
          if (i % 6 == 0 && i != 0) {
            k += 1;
            tomb[x] = k++;
          } else {
            tomb[x] = k++;
          }
        }
        if (i >= 6) {
          if (i % 6 == 0 && i != 0) {
            tomb[x] = k++;
            k += 1;
          } else {
            tomb[x] = k++;
          }
        }
        if (i == 35) {
          tomb[x] = 36;
        }
        x++;
      }
    
      FILE *outfile = fopen("ki.txt", "w");
      i = 0;
      while (str[i] != '\0') {
        if (!((str[i] >= 0 && str[i] <= 31) || (str[i] >= 33 && str[i] < 65)
             || (str[i] > 90 && str[i] < 97) || (str[i] > 122 && str[i] <= 127))) {
          if (str[i] >= 'A' && str[i] <= 'Z') {
            fprintf(outfile, "%d\n ", tomb[str[i] - 'A']);
          }
          if (str[i] >= 'a' && str[i] <= 'z') {
            fprintf(outfile, "%d\n ", tomb[str[i] - 'a']);
          }
        }
        if (str[i] == ' ') {
          fprintf(outfile, "%c\n", str[i]);
        }
        i++;
      }
    }
    > for (i = 0; i <= 36; i++)
    Then I think you're running off the end of your array.

    > if (!((str[i] >= 0 && str[i] <= 31) || (str[i] >= 33 && str[i] < 65)
    > || (str[i] > 90 && str[i] < 97) || (str[i] > 122 && str[i] <= 127)))
    Then I'm wondering whether this is just a complicated way of saying isalpha() or complete gibberish.

    > if (i <= 6)
    > if (i >= 6)
    So what happens when i == 6 ?



    Perhaps you could print every entry of tomb[x] when you're done filling the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-04-2014, 09:51 AM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  4. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM

Tags for this Thread