Thread: C Program logic error?

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Please post your latest code.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ok, you know what it does by now or should i explain again?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define LINES 5
    
    /* assignment #1 function prototype */
    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 ^*@&#^% 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, 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';  
    	    }
          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("%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(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]; 
                          }
                    }
                 
                 rpt1 = atoi (rpt);
                 //printf("\n\n%d\n\n", rpt1);
                 len = strlen(binarray);
                 for(rptcounter=1; rptcounter<rpt1; rptcounter++){ //loops rpt1 times
                    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;
    }

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmac(rexdale) View Post
    ok, you know what it does by now or should i explain again?
    Quote Originally Posted by tabstop View Post
    You forgot to tell us what problems your current code is having.
    Does printing the value rpt always give you the right answer? atoi requires the string to be null-terminated, and I don't see anywhere where you null terminate rpt. And there is still no such variable in your code as rpt[4], despite your attempt to assign to such.

    And your for-loop is still off: you need to choose between counting from 0 (and using <) and counting from 1 (and using <=).

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Quote Originally Posted by tabstop View Post
    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).
    the problem is when the program runs it gives me the wrong output

    rpt is the number of times to repeat the same number, so in the last test it wants 011111 repeated 10 times, the output gives me 9 correct characters (01111100 in ascii is '|') but it give me '~' for the last but the last one is suppose to be '|' also

    as for the for loop when i change it to what you said and others also, my second test doesn't work. so if i change the for loop one test will work, if i change it again the same one doesn't

    i put rpt[3]='\0' here:
    Code:
    if(core[i+1] == 'P' && core[i+2] == 'T') {         
              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]; 
                          }
                    }
                 rpt[3] = '\0';
    edit: and i took out rpt[4]='\0', i dont know why i put it there
    Last edited by bigmac(rexdale); 02-14-2008 at 03:03 PM.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmac(rexdale) View Post
    the problem is when the program runs it gives me the wrong output

    rpt is the number of times to repeat the same number, so in the last test it wants 011111 repeated 10 times, the output gives me 9 correct characters (01111100 in ascii is '|') but it give me '~' for the last but the last one is suppose to be '|' also

    as for the for loop when i change it to what you said and others also, my second test doesn't work. so if i change the for loop one test will work, if i change it again the same one doesn't

    i put rpt[3]='\0' here:
    Code:
    if(core[i+1] == 'P' && core[i+2] == 'T') {         
              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]; 
                          }
                    }
                 rpt[3] = '\0';
    edit: and i took out rpt[4]='\0', i dont know why i put it there
    Your other test doesn't work either. However, it may look like it does because your RPT code does none of the following:
    1. advances i past the repeat numbers (so that if you have, for instance, RPT10, the 1 and 0 are read in later as "real" binary characters)
    2. "clears out" the data (by, for instance, resetting j so that the next character starts out from scratch).

    So in test #2, since RPT2 is the last code, your RPT loop prints out the character only once, but the character is still there, so the end-of-loop-code takes care of printing out the second one. In test #4 or 5, I forget which, you print one too few, but then read the 10 as binary digits, which fill out the rest of the code (since that one was two short to begin with) and the last one prints out, not as xxxxxx00 but as xxxxxx10.

    So if that design of one-short-and-let-the-normal-input-handler-print-the-rest was deliberate, well, your instructor was ready for you. It will behoove you to make sure your RPT loop does both one and two above.

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    so, my logic for finding rpt is wrong then and i should add some code for?:

    "1. advances i past the repeat numbers (so that if you have, for instance, RPT10, the 1 and 0 are read in later as "real" binary characters)
    2. "clears out" the data (by, for instance, resetting j so that the next character starts out from scratch)."

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmac(rexdale) View Post
    so, my logic for finding rpt is wrong then
    You find rpt (and the number behind it) just fine.
    Quote Originally Posted by bigmac(rexdale) View Post
    and i should add some code for?:

    "1. advances i past the repeat numbers (so that if you have, for instance, RPT10, the 1 and 0 are read in later as "real" binary characters)
    2. "clears out" the data (by, for instance, resetting j so that the next character starts out from scratch)."
    Only if you want your program to work correctly.

    Edit to add: Oh, and you may want to make sure that you set, not necessarily rpt[3] to \0, but whatever the next character is. (For instance, if you hit an RPT10 and then an RPT2, you'll overwrite the 1 with the 2, but you never get rid of the 0, so you'll actually end up doing RPT20.)
    Last edited by tabstop; 02-14-2008 at 03:37 PM.

  8. #23
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i did this is this is what you meant by advancing i past
    Code:
    if(core[i+1] == 'P' && core[i+2] == 'T') {
              i=i+3;                   
              if(isdigit(core[i])) {
                 rpt[0] = core[i];
                 i++;
                  
                    if(isdigit(core[i])) { 
                       rpt[1] = core[i];
                       i++;   
                          if(isdigit(core[i])) {
                             rpt[2] = core[i];
                             i++; 
                          }
                    }
    for clearing out data, i put "j = 0;" while the loop is looping

    i added these things and i get a different result but its still not right, its gives me 9 instead of ten '|' and the '}' but i need one more '|' before the '}'

    edit: actually, the for loop needed to be changed also, it works now all 5 tests have passed thanks for your help!
    Last edited by bigmac(rexdale); 02-14-2008 at 03:38 PM.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmac(rexdale) View Post
    i did this is this is what you meant by advancing i past
    Code:
    if(core[i+1] == 'P' && core[i+2] == 'T') {
              i=i+3;                   
              if(isdigit(core[i])) {
                 rpt[0] = core[i];
                 i++;
                  
                    if(isdigit(core[i])) { 
                       rpt[1] = core[i];
                       i++;   
                          if(isdigit(core[i])) {
                             rpt[2] = core[i];
                             i++; 
                          }
                    }
    for clearing out data, i put "j = 0;" while the loop is looping

    i added these things and i get a different result but its still not right, its gives me 9 instead of ten '|' and the '}' but i need one more '|' before the '}'
    Did you fix the for loop?

    Edit to add: And yes, that is what you need to do to get i past the RPT characters.

  10. #25
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Quote Originally Posted by tabstop View Post
    Did you fix the for loop?

    Edit to add: And yes, that is what you need to do to get i past the RPT characters.
    yes i fixed it
    Code:
    for(rptcounter=0; rptcounter<rpt1; rptcounter++)
    makes the program run correctly

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigmac(rexdale) View Post
    yes i fixed it
    Code:
    for(rptcounter=0; rptcounter<rpt1; rptcounter++)
    makes the program run correctly
    Yay! I hope I never have to see this program again.

  12. #27
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Quote Originally Posted by tabstop View Post
    Yay! I hope I never have to see this program again.
    haha, yeah i think you were the one that helped me get this program started like 1 month ago

  13. #28
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    You got it?? Yay! Here check this out for a comparison:
    (Notice the indentation!)
    Code:
    int bit_to_ascii(const char input[], char output[]) {
      const char *src = input;
      char *dst = output;
      int value = 0;
      int num_bits = 0;
      int repeat = 1;
      
      while (*src != '\0') {
        switch (*src++) {
          case '0':
            value += value; num_bits++;
            break;
          case '1':
            value += value + 1; num_bits++;
            break;
          case 'S':
            if (src[0] == 'K' && src[1] == 'P') {
              int skip = 8;
              while (skip > 0) {
                if (*src == '0' || *src == '1')
                  skip--;
                src++;
              }
            }
            break;
          case 'R':
            if (src[0] == 'P' && src[1] == 'T') {
              src += 2;
              repeat = 0;
              while (isdigit(*src))
                repeat = (repeat * 10) + (*src++ - '0');
                
              if (num_bits == 0) {
                char last = *(dst - 1);
                for (repeat; repeat > 1; repeat--)
                  *dst++ = last;
              }
              else
                while (num_bits < 8) {
                  value <<= 1;
                  num_bits++;
                }
            }
            break;
        }
        
        if (num_bits > 7) {
          if (isprint(value) || value == 0)
            for (repeat++; repeat > 1; repeat--)
              *dst++ = (char)value;
          num_bits = 0;
          value = 0;
        }
      }
      
      if (num_bits > 0 && (isprint(value) || value == 0))
        *dst++ = value;
      
      return (int)(dst - output);
    }
    Last edited by arpsmack; 02-15-2008 at 11:23 AM.

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, 03:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04: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