C Program logic error?

This is a discussion on C Program logic error? within the C Programming forums, part of the General Programming Boards category; I've been writing this program for a good 2 months know, all it is is a function and my teacher ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    C Program logic error?

    I've been writing this program for a good 2 months know, all it is is a function and my teacher supplies the int main, so here it is with the supplied int main and my function.

    Before i show it, this is what its suppose to do. It looks
    "for the characters '1' and '0' which, when taken 8 at a time and converted
    to their numeric value in decimal, presumably encode a meaningful ascii
    character, and inserts that character into the array "data""

    When it finds "RPT followed by a number" ie (RPT5), then the program has to repeat the 8 1's and 0's 5 times. If it finds SKP, the next 8 1's and 0's are skipped.

    ie. 01010101RPT401010101SKP10101010

    in that ^^ 01010101 will be repeated 4 times, then 01010101 and 10101010 will be skipped

    problem is, on the second check of core_data, it stops when it reaches 'c'

    Code:
    int bit_to_ascii(const char [ ], char [ ]);
    
    int main( ) {
    
       const char *core_data[ ] = {
    "0", /* should insert the null byte '\0' only */
    
    "01010111 This sequence contains the letter 'W'       " /*  87 = 'W' */
    "      r5 0 #! 1 1 gP 0 f66-:] 0 [ } 1 v4t^ 0 1 n>?:77" /* 101 = 'e' */
    " junk ^*@&#^&#37; 0110  more junk }{r;,<> 1100         2x" /* 108 = 'l' */
    "                  0    1       1     0 0          011" /*  99 = 'c' */
    "0  ^%jh  1 ]{) OVR 1 - 0 _! 9 + 1  118Y z(x pM1      " /* 111 = 'o' */
    "This next group of 8 should be SKP skipped! 11111111 " /* skipped */
    "*&^! 0  1  1  0 1     1 0     128673kjshd:L<    2938 " /* 109 = 'm' */
    "      r5 0 #! 1 1 gP 0 f66-:] 0 [ } 1 v4t^ 0 1 n>?:77" /* 101 = 'e' */
    "#!/bin/bash 00  1 00 00 0 g^b-ps 6@&^%               " /*  32 = ' ' */
    "01010100 This sequence contains the letter 'T'       " /*  84 = 'T' */
    "0  ^%jh  1 ]{) OVR 1 - 0 _! 9 + 1  118Y z(x pM1      " /* 111 = 'o' */
    "#!/bin/bash 00  1 00 00 0 g^b-ps 6@&^%               " /*  32 = ' ' */
    "---repeat--->0 1 0 0 1 1 1 1<------------RPT2--------" /*  79 = 'O' */
    "0 1 0 1 0bbavac9872962  000&*************************" /*  80 = 'P' */
    "a0b0c1d1e0f0g1h0ijklmnopqrstuvwxyz*******************" /*  50 = '2' */
    "00fg333                                110100>>>>>>>>" /*  52 = '4' */
    "00fg333                                110100>>>>>>>>" /*  52 = '4' */
    "                     0000                            ",/*  0 = '\0' */
    
    "00011110 This char should not be inserted!           " /*  30 cntrl */
    " 00011111 This char should not be inserted!          " /*  31 cntrl */
    "  00100000                                           " /*  32 = ' ' */
    "   00100001                                          " /*  33 = '!' */
    "    00100010                                         " /*  34 = '"' */
    "     00100011                                        " /*  35 = '#' */
    "      00100100                                       " /*  36 = '$' */
    "       00100101                                      " /*  37 = '%' */
    "        00100110                                     " /*  38 = '&' */
    "         00100111                                    " /*  39 = ''' */
    "          00101000                                   " /*  40 = '(' */
    "           00101001                                  " /*  41 = ')' */
    "            00101010                                 " /*  42 = '*' */
    "             00101011                                " /*  43 = '+' */
    "              00101100                               " /*  44 = ',' */
    "               00101101                              " /*  45 = '-' */
    "                00101110                             " /*  46 = '.' */
    "                 00101111                            " /*  47 = '/' */
    "                  00110000                           " /*  48 = '0' */
    "                   00110001                          " /*  49 = '1' */
    "                    00110010                         " /*  50 = '2' */
    "                     00110011                        " /*  51 = '3' */
    "                      00110100                       " /*  52 = '4' */
    "                       00110101                      " /*  53 = '5' */
    "                        00110110                     " /*  54 = '6' */
    "                         00110111                    " /*  55 = '7' */
    "                          00111000                   " /*  56 = '8' */
    "                           00111001                  " /*  57 = '9' */
    "                            00111010                 " /*  58 = ':' */
    "                             00111011                " /*  59 = ';' */
    "                              00111100               " /*  60 = '<' */
    "                               00111101              " /*  61 = '=' */
    "                                00111110             " /*  62 = '>' */
    "                                 00111111            " /*  63 = '?' */
    "                                  01000000           " /*  64 = '@' */
    "                                   00000000          ",/*   0 = '\0'*/
    
    "                                    01000001         " /*  65 = 'A' */
    "                                     01000010        " /*  66 = 'B' */
    "                                      01000011       " /*  67 = 'C' */
    "                                       01000100      " /*  68 = 'D' */
    "                                        01000101     " /*  69 = 'E' */
    "                                         01000110    " /*  70 = 'F' */
    "                                          01000111   " /*  71 = 'G' */
    "                                           01001000  " /*  72 = 'H' */
    "                                            01001001 " /*  73 = 'I' */
    "                                             01001010" /*  74 = 'J' */
    "                                            01001011 " /*  75 = 'K' */
    "                                           01001100  " /*  76 = 'L' */
    "                                          01001101   " /*  77 = 'M' */
    "                                         01001110    " /*  78 = 'N' */
    "                                        01001111     " /*  79 = 'O' */
    "                                       01010000      " /*  80 = 'P' */
    "                                      01010001       " /*  81 = 'Q' */
    "                                     01010010        " /*  82 = 'R' */
    "                                    01010011         " /*  83 = 'S' */
    "                                   01010100          " /*  84 = 'T' */
    "                                  01010101           " /*  85 = 'U' */
    "                                 01010110            " /*  86 = 'V' */
    "                                01010111             " /*  87 = 'W' */
    "                               01011000              " /*  88 = 'X' */
    "                              01011001               " /*  89 = 'Y' */
    "                             01011010                " /*  90 = 'Z' */
    "                            01011011                 " /*  91 = '[' */
    "                           01011100                  " /*  92 = '\' */
    "                          01011101                   " /*  93 = ']' */
    "                         01011110                    " /*  94 = '^' */
    "                        01011111                     " /*  95 = '_' */
    "                       01100000                      " /*  96 = '`' */
    "                      00000000                       ",/*   0 = '\0'*/
    
    "                     01100001                        " /*  97 = 'a' */
    "                    01100010                         " /*  98 = 'b' */
    "                   01100011                          " /*  99 = 'c' */
    "                  01100100                           " /* 100 = 'd' */
    "                 01100101                            " /* 101 = 'e' */
    "                01100110                             " /* 102 = 'f' */
    "               01100111                              " /* 103 = 'g' */
    "              01101000                               " /* 104 = 'h' */
    "             01101001                                " /* 105 = 'i' */
    "            01101010                                 " /* 106 = 'j' */
    "           01101011                                  " /* 107 = 'k' */
    "          01101100                                   " /* 108 = 'l' */
    "         01101101                                    " /* 109 = 'm' */
    "        01101110                                     " /* 110 = 'n' */
    "       01101111                                      " /* 111 = 'o' */
    "      01110000                                       " /* 112 = 'p' */
    "     01110001                                        " /* 113 = 'q' */
    "    01110010                                         " /* 114 = 'r' */
    "   01110011                                          " /* 115 = 's' */
    "  01110100                                           " /* 116 = 't' */
    " 01110101                                            " /* 117 = 'u' */
    "01110110                                             " /* 118 = 'v' */
    " 01110111                                            " /* 119 = 'w' */
    "  01111000                                           " /* 120 = 'x' */
    "   01111001                                          " /* 121 = 'y' */
    "    01111010                                         " /* 122 = 'z' */
    "     01111011                                        " /* 123 = '{' */
    "      011111RPT10                                    " /* 124 = '|' */
    "       01111101                                      " /* 125 = '}' */
    "        00000000                                     " /*   0 = '\0'*/
    };
    
       char phrase[41];
       char correct[LINES][41] = {
          "",
          "Welcome To OOP244",
          " !\"#$%&'()*+,-./0123456789:;<=>?@",
          "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
          "abcdefghijklmnopqrstuvwxyz{||||||||||}"
       };
    
       int rvalues[LINES] = { 1, 18, 34, 33, 39 }, i;
       int valid = 1, rc, err_code;
       
       for(i=0; i<LINES && valid; i++) {
          rc = bit_to_ascii(core_data[i], phrase);
          if(rc != rvalues[i] || strcmp(phrase, correct[i])) {
    	 printf("Failed on test... %d\n\n", i+1);
    	 valid = 0;
          }
          else {
    	 printf("Passed test %d...\n\n", i+1);
          }
          printf("Your string ----------> '%s'\n", phrase);
          printf("Actual string --------> '%s'\n", correct[i]);
          printf("Your return value ----> %d\n", rc);
          printf("Actual return value --> %d\n", rvalues[i]);
          printf("Press the ENTER key to continue...");
          getchar( );
       }
       if(valid) {
          printf("\nCongratulations!!! You passed all tests.\n");
          printf("You may hand in your assignment.\n");
       }
       else {
          printf("\nYou correctly sequenced %d/5 data blocks...\n", i-1);
          printf("Your program still needs some work!\n");
          printf("Keep at it!\n\n");
       }
       return 0;
    }
    
    int bit_to_ascii(const char core[ ], char data[ ]) {
       int i, rc = 0, j, inte[7], len, len2, m, lenrpt, rpt1, rptcounter, bin, lendata = 0, p = 0;
       char binarray[9], rpt[4];
       rpt[4] = '\0';
       for(i=0, j=0; core[i] != '\0'; i++) {
         switch(core[i]) {
    	 case '1':
    	 case '0':
      	  if(j<8) {
    	       binarray[j++] = core[i];
    	    }
          else {
             binarray[j] = '\0';  
             inte[0] = (binarray[0] - 48) * 128;
             inte[1] = (binarray[1] - 48) * 64;
             inte[2] = (binarray[2] - 48) * 32;
             inte[3] = (binarray[3] - 48) * 16;
             inte[4] = (binarray[4] - 48) * 8;
             inte[5] = (binarray[5] - 48) * 4;
             inte[6] = (binarray[6] - 48) * 2;
             inte[7] = (binarray[7] - 48) * 1;
             bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
             data[lendata] = bin;
             lendata++;
       
             printf("%d %d %d %d %d %d %d %d\n", inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
           j = 0; /* resets j to 0 after 8 1's or 0's have been found */
           if(j<8) {
    	       binarray[j++] = core[i]; 
    	    }//end if
        }//end else
        case 'R':
           if(core[i+1] == 'P' && core[i+2] == 'T') {
              if(core[i+3]>='0' || core[i+3]<='9') {
                 rpt[0] = core[i+3];
                  
                    if(core[i+4]>='0' || core[i+4]<='9') { 
                       rpt[1] = core[i+4];
                          
                          if(core[i+5]>='0' || core[i+5]<='9') {
                             rpt[2] = core[i+5]; 
                          }
                    } 
                 lenrpt = strlen(rpt);
                 
                  
                 rpt1 = atoi (rpt);
                 for(rptcounter=1; rptcounter!=rpt1; rptcounter++){
                    len = strlen(binarray);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              do {
                                          binarray[m]='0';
                                          m++;
                              }while (m != 8);            
                              }
                    inte[0] = (binarray[0] - 48) * 128;
                    inte[1] = (binarray[1] - 48) * 64;
                    inte[2] = (binarray[2] - 48) * 32;
                    inte[3] = (binarray[3] - 48) * 16;
                    inte[4] = (binarray[4] - 48) * 8;
                    inte[5] = (binarray[5] - 48) * 4;
                    inte[6] = (binarray[6] - 48) * 2;
                    inte[7] = (binarray[7] - 48) * 1;
                    bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
                    data[lendata] = bin;
                    lendata++;
                    
                    printf("%d %d %d %d %d %d %d %d\n", inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
                 }                                
              }   
           }
        break;
        case 'S':
           if(core[i+1] == 'K' && core[i+2] == 'P') {
           i = i + 3;
                 do {             
                    switch(core[i]) {
    	              case '1':
    	              case '0': 
                      p++;
                      i++;
                      break;
                    }   
                 } while (p<7);         
                                     
           }
        break;     
          }//end case
          }//end for
       binarray[j] = '\0';
       len = strlen(binarray);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              do {
                                          binarray[m]='0';
                                          m++;
                              }while (m != 8);            
                              }
       inte[0] = (binarray[0] - 48) * 128;
       inte[1] = (binarray[1] - 48) * 64;
       inte[2] = (binarray[2] - 48) * 32;
       inte[3] = (binarray[3] - 48) * 16;
       inte[4] = (binarray[4] - 48) * 8;
       inte[5] = (binarray[5] - 48) * 4;
       inte[6] = (binarray[6] - 48) * 2;
       inte[7] = (binarray[7] - 48) * 1;
       bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
       data[lendata] = bin;
       lendata++;
       data[lendata]='\0';
          
       printf("%d %d %d %d %d %d %d %d\n", inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
       
       printf("\nBit Converted to ASCII is: %s\n", data);
       rc = strlen(data);
       return rc;
    }
    Last edited by bigmac(rexdale); 02-11-2008 at 01:16 PM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    You have some minor indenting problems that make your code hard to read. Maybe you're mixing tabs and spaces??

    The first thing I would point out is that there's a much better way to build your character code from the binary input. Rather than keeping track of 8 '1's and '0's in a buffer, and doing all the math when the buffer gets full, why not try this approach:

    Observe what happens when this sequence gets read in 0 0 1 1 0 1 1 0

    Value = 00000000 <- The initial value is set to zero
    Value = 00000000 (read a zero)
    Value = 00000000 (read a zero)
    Value = 00000001 (read a one)
    Value = 00000011 (read a one)
    Value = 00000110 (read a zero)
    Value = 00001101 (read a zero)
    ...

    See how each of the read in binary digits causes the value to shift over one place? Use that to your advantage. Combine this with a count of how many ones and zero's you've seen so far and you'll know when to "print" the value to the output buffer.

    You can use a similar approach when reading in the digits for the RPT case. Read the first digit into an integer variable. If the next character is a digit, "shift" your integer variable and add the new digit (This is called Horner's Algorithm). This will reduce your reliance on the rpt buffer that you pass to atoi().

    I think you'll see that this approach cuts down on a lot of the "clutter" making the code easier to read and manage. You can write a (more efficient) solution to this using only 7 variables (none of which are arrays) and about half as much code, maybe even less.

    On a minor side note, I did notice this:
    Code:
    if(core[i+3]>='0' || core[i+3]<='9') {
       ...
    }
    This is almost certainly not what you meant. You need an AND operator in there, not an OR. That boolean expression will match ANY character you give it. The instructor's test case didn't give you a RPT followed by a non-digit, but if you test it I think you'll see that it chokes or gives strange results. A MUCH better solution would be to use the isdigit() function from ctype.h:
    Code:
    if (isdigit(core[i+3])) {
       ...
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Nope. All spaces. Either it was messed up during copy n' paste or you need to learn how to indent a little.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    When i saw the first lines it looked like an IOCC entry, but then it was quite simple. There must be better ways to teach someone to parse text. I mean, it is brutal. There are so many tools out there to do things like that. C provides only the raw basics except if you punch in a good regex library.
    I would suggest the student, to ask the teacher to be taught some regular expressions. It is a much more interesting and widely usable knowledge, than finding 8bit binary numbers written in a big garbled ascii string, which again doesn't teach you much of the C language potential.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ^^ Haven't been taught that and this assignment is for my C++ class. But i fixed a most of the problems i was having.

    One last problem if you wanna try it out, copy this function into the main in my above post. It passes test 1, 2, 3 and 4 but on 5 it doesn't past and it looks like the logic used for RPT is the reason why:
    Code:
    int bit_to_ascii(const char core[ ], char data[ ]) {
       int i, rc = 0, j, inte[7], len, len2, m, lenrpt, rpt1, rptcounter, bin, lendata = 0, p = 0, skp1 = 0;
       char binarray[9], rpt[4], skp[9];
       rpt[4] = '\0';
       for(i=0, j=0; core[i] != '\0'; i++) {
         switch(core[i]) {
    	 case '1':
    	 case '0':
      	  if(j<8) {
    	       binarray[j++] = core[i];
    	    }
          else {
             binarray[j] = '\0';
             inte[0] = (binarray[0] - 48) * 128;
             inte[1] = (binarray[1] - 48) * 64;
             inte[2] = (binarray[2] - 48) * 32;
             inte[3] = (binarray[3] - 48) * 16;
             inte[4] = (binarray[4] - 48) * 8;
             inte[5] = (binarray[5] - 48) * 4;
             inte[6] = (binarray[6] - 48) * 2;
             inte[7] = (binarray[7] - 48) * 1;
             
             bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
             if(bin<=0 || bin >=32) {
             data[lendata] = bin;
             lendata++;
             
       
             printf("&#37;d %d %d %d %d %d %d %d\n", inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
             }
             j = 0; /* resets j to 0 after 8 1's or 0's have been found */
             
           if(j<8) {
    	       binarray[j++] = core[i]; 
    	    }//end if
        }//end else
        
        case 'R':
             
           if(core[i+1] == 'P' && core[i+2] == 'T') {
              if(core[i+3]>='0' || core[i+3]<='9') {
                 rpt[0] = core[i+3];
                  
                    if(core[i+4]>='0' || core[i+4]<='9') { 
                       rpt[1] = core[i+4];
                          
                          if(core[i+5]>='0' || core[i+5]<='9') {
                             rpt[2] = core[i+5]; 
                          }
                    }
                     
                 //lenrpt = strlen(rpt);
                 
                  
                 rpt1 = atoi (rpt);
                 for(rptcounter=1; rptcounter!=rpt1; rptcounter++){ //loops rpt1 times
                    len = strlen(binarray);
                    printf("%d", len);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              do {
                                          binarray[m]='0';
                                          m++;
                              }while (m != 8);            
                              }
                                          
                    inte[0] = (binarray[0] - 48) * 128;
                    inte[1] = (binarray[1] - 48) * 64;
                    inte[2] = (binarray[2] - 48) * 32;
                    inte[3] = (binarray[3] - 48) * 16;
                    inte[4] = (binarray[4] - 48) * 8;
                    inte[5] = (binarray[5] - 48) * 4;
                    inte[6] = (binarray[6] - 48) * 2;
                    inte[7] = (binarray[7] - 48) * 1;
                    bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
                    data[lendata] = bin;
                    lendata++;
                    
                    printf("%d %d %d %d %d %d %d %d\n", inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
                 }                                
              }   
           }
        break;
        case 'S':
           if(core[i+1] == 'K' && core[i+2] == 'P') {
           i=i+3;
           for(p=0;p!=8;i++) {
              switch(core[i]) {
    	      case '1':
    	      case '0':
                   skp[p] = core[i];
                   p++;
                   break;
              }
           }                          
           }
        break;     
          }//end case
          }//end for
       binarray[j] = '\0';
       len = strlen(binarray);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              do {
                                          binarray[m]='0';
                                          m++;
                              }while (m != 8);            
                              }
       inte[0] = (binarray[0] - 48) * 128;
       inte[1] = (binarray[1] - 48) * 64;
       inte[2] = (binarray[2] - 48) * 32;
       inte[3] = (binarray[3] - 48) * 16;
       inte[4] = (binarray[4] - 48) * 8;
       inte[5] = (binarray[5] - 48) * 4;
       inte[6] = (binarray[6] - 48) * 2;
       inte[7] = (binarray[7] - 48) * 1;
       bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
       data[lendata] = bin;
       lendata++;
       data[lendata]='\0';
       printf("%d %d %d %d %d %d %d %d\n", inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
       
       printf("\nBit Converted to ASCII is: %s\n", data);
       rc = strlen(data) + 1;
       if (rc==0) {
                  rc++;
                  }
       return rc;
    }
    as for your approach arpsmack, it shouldnt be to much work to go back and re-write my code accoring to your approach should it?

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    It shouldn't be too difficult if you understand Horner's Algorithm and how to "shift" a binary or decimal number to the left one digit. Here's a brief outline to get you started:
    Code:
    switch (next_character) {
       case '0':
          shift value to the left one place
          increase bit number count
          break
    
       case '1':
          shift value to the left one place and add 1
          increase bit number count
          break
    
       ...
    }
    
    once you've read in 8 bits (You're keeping track of that with your bit count),
    the exact character code you need will be stored in your value variable, you
    can check if its a legal character and "print" it to the output buffer the
    proper number of times
    I think you can probably figure out the rest.

    Keep in mind though that you didn't fix the problem I mentioned in your RPT code:
    Code:
    if(core[i+3]>='0' || core[i+3]<='9') { // Uh oh
       rpt[0] = core[i+3];
    
       if(core[i+4]>='0' || core[i+4]<='9') { // Uh oh
          rpt[1] = core[i+4];
    
          if(core[i+5]>='0' || core[i+5]<='9') { // Uh oh again
             rpt[2] = core[i+5]; 
          }

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    alright i fixed that and used the isdigit you mentioned

    Code:
    if(isdigit(core[i+3])) {
                 rpt[0] = core[i+3];
                  
                    if(isdigit(core[i+4])) { 
                       rpt[1] = core[i+4];
                          
                          if(isdigit(core[i+5])) {
                             rpt[2] = core[i+5]; 
                          }
                    }
    maybe there is a logic error in case 'R'??

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Well upon closer inspection, it appears that your RPT loop has an off-by-one error.
    Code:
    for (rptcounter = 1; rptcounter != rpt1; rptcounter++) { //loops rpt1 times
       ...
       // Despite what your comment says, this code will only loop
       // (rpt1 - 1) times.  Could this be your problem?
    }
    I'm not sure what your problem is exactly, so its kinda hard to locate the error. Is it printing a character too many times? Are you getting strange output (unexpected characters in the output)?

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    it's giving the wrong result but the number of characters are correct.

    you see the core_data char has 5 arrays, so the function bit_to_ascii is going to be called 5 times, it passes on the 1st array when core_data = "0", it also passes 2, 3 and 4

    but on the last array:
    Code:
    "                     01100001                        " /*  97 = 'a' */
    "                    01100010                         " /*  98 = 'b' */
    "                   01100011                          " /*  99 = 'c' */
    "                  01100100                           " /* 100 = 'd' */
    "                 01100101                            " /* 101 = 'e' */
    "                01100110                             " /* 102 = 'f' */
    "               01100111                              " /* 103 = 'g' */
    "              01101000                               " /* 104 = 'h' */
    "             01101001                                " /* 105 = 'i' */
    "            01101010                                 " /* 106 = 'j' */
    "           01101011                                  " /* 107 = 'k' */
    "          01101100                                   " /* 108 = 'l' */
    "         01101101                                    " /* 109 = 'm' */
    "        01101110                                     " /* 110 = 'n' */
    "       01101111                                      " /* 111 = 'o' */
    "      01110000                                       " /* 112 = 'p' */
    "     01110001                                        " /* 113 = 'q' */
    "    01110010                                         " /* 114 = 'r' */
    "   01110011                                          " /* 115 = 's' */
    "  01110100                                           " /* 116 = 't' */
    " 01110101                                            " /* 117 = 'u' */
    "01110110                                             " /* 118 = 'v' */
    " 01110111                                            " /* 119 = 'w' */
    "  01111000                                           " /* 120 = 'x' */
    "   01111001                                          " /* 121 = 'y' */
    "    01111010                                         " /* 122 = 'z' */
    "     01111011                                        " /* 123 = '{' */
    "      011111RPT10                                    " /* 124 = '|' */
    "       01111101                                      " /* 125 = '}' */
    "        00000000                                     " /*   0 = '\0'*/
    the bolded part is where i get the wrong characters. This is the result, look where it says "Your string" and "Actual string" and you can see what is different
    http://img181.imageshack.us/img181/1563/77504265gp3.png

    edit: when i print binarray before i RPT binarray is 01111111, but, its suppose to be 011111 (referring to bolded part) and two 0's added because the lenght is less than 8...the lenght is 6
    Last edited by bigmac(rexdale); 02-12-2008 at 03:15 PM.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Ok I see. One reason for the error is that you aren't properly terminating the binarray. Try adding this:

    Code:
    int bit_to_ascii(const char core[ ], char data[ ]) {
      int i, rc = 0, j, inte[7], len, len2, m, lenrpt, rpt1, rptcounter, bin, lendata = 0, p = 0, skp1 = 0;
      char binarray[9], rpt[4], skp[9];
      rpt[4] = '\0';
      for(i=0, j=0; core[i] != '\0'; i++) {
        switch(core[i]) {
          case '1':
          case '0':
            if(j<8) {
              binarray[j++] = core[i];
              binarray[j] = '\0';
            }
    ...
    If you don't keep your binarray in a properly null-terminated state, then this code in your RPT logic won't work:
    Code:
    for(rptcounter=1; rptcounter!=rpt1; rptcounter++){ //loops rpt1 times
       len = strlen(binarray);  // This relies on binarray being null-terminated in the proper spot
    With that tiny modification I now get the following output:
    Code:
    Your string ----------> 'abcdefghijklmnopqrstuvwxyz{|||||||||}˘'
    Actual string --------> 'abcdefghijklmnopqrstuvwxyz{||||||||||}'
    And the reason this is printing wrong is probably due the the off-by-one error I mentioned earlier.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    thanks alot man, i got one problem fixed but the off-by-one problem makes my other test not work

    for(rptcounter=0; rptcounter!=rpt1; rptcounter++){

    i made rptcounter=0, but it adds a extra O in test #2

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    I apologize as this may sound rude, but I really think the last bit is something you should fix yourself.

    Step through the code in your head (or in a debugger) as you're processing the input in question and I think you'll see how you can deal with the off-by-one issue.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Do you think that could be the problem though, because in the test#2 it says RPT2 and that passes without a problem.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i'm still stuck, i dont know whats wrong with my code anyone can give some advice?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You forgot to tell us what problems your current code is having.

    My Hat of Guessing wants to remind you that, just like you need to keep your binarray in a properly null-terminated state, you need to keep your rpt[] character array in a properly null-terminated state (and also that if you define rpt by char rpt[4], then you had darn well better not assign anything, even '\0', to rpt[4] on the next line).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

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