Thread: help with walkthrough

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    help with walkthrough

    hi guys i am having trouble with a walk through. The part i am having trouble with is the c_lang class print function. I don't understand what str[shift] = str[shift] - (x - '0'); is doing. Specifically the '0' is messing me up. The first character is send in is an x. so i do str[0]=x(the string at 0)-('x'-'0'). This some how becomes v and i have no clue why.


    Code:
    class c_lang {
       protected:
          int shift;
          char *str;
       public:
          c_lang(char *s) {
             str = s;   // storing the ADDRESS of 's' (be careful!)
             shift = 0;
             cout << "#include <stdio.h>" << endl;
          }
          virtual void print(char x) {
             str[shift] = str[shift] - (x - '0');
             shift++;
          }
          virtual void operator<<(char *);
          ~c_lang( ) { cout << str << endl; }
    };
    void c_lang::operator<<(char *s) {
       for(int i=0; s[i]; i++) {
          str[i] = str[i] - 2;   cout << s[i];
       }
    }
    
    class c_plus_plus : public c_lang { // derived from base
       protected:                       // class (2nd level)
          int len, pos;
       public:
          c_plus_plus(char *s, int p) : c_lang(s) {
             len = strlen(s);
             pos = p;
             cout << "using namespace::std;" << endl;
          }
          virtual void print(char y) {
             str[pos] = str[pos] + (y - '0');
             cout << str << endl;
          }
          virtual void operator<<(char *);
    };
    void c_plus_plus::operator<<(char *s) {
       for(int i=0; i<len; i++) {
          print(s[i]);
          pos++;
       }
    }
    
    class c_sharp : public c_plus_plus { // derived from previously
                                         // derived class (3rd level)
       public:
          c_sharp(char *s, int p) : c_plus_plus(s, p) {
             cout << "using System;" << endl;
          }
          virtual void operator<<(char *);
    };
     
    void c_sharp::operator<<(char *s) {
       for(int i=0; i<len; i++) {
          c_lang::print(s[i]);
       }
    }
    
    void compile(c_lang &r, char *s) {
       r << s;
    }
    
    int main( ) {
       char L1[10]="xszfiis 9", L2[10]="kba-0301b";
       c_sharp cs(L1, 0);
       c_plus_plus cpp(L2, 0);
     
       compile(cs,  "241524103");
       compile(cpp, "312014002");
       cout << "---------" << endl;
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Thegame16,

    I briefly looked through the board and found this discussion: variable type conversion. It pertains directly to your inquiry, and points some more formal procedures for type casting. Recent C++ standards have several elaborate mechanisms for performing these tasks. Because type-casting is always an enlightening topic, and I hope some one corrects me if I am wrong. The specific line about which you're asking, namely...

    Code:
    str[shift] = str[shift] - (x - '0');
    ...is performing several conversions. First the character x is converted to it's integer value, and then the integer value of zero (48) is deducted from it. The resulting quantity is subtracted from the integer value of the character at location shift of the character pointer str. Finally, this integer is converted back to a character and assigned to the location shift of the character pointer str.

    A table of the integer values of characters may be found in many reference text books, and is available in a Wikipedia article: ASCII - Wikipedia, the free encyclopedia.

    When you try to run this code, please don't forget to include the standard io library and declare the name space....

    Best Regards,
    Last edited by new_ink2001; 04-18-2011 at 10:37 PM. Reason: syntax
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mental walkthrough
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2004, 01:59 PM
  2. Peasant's Quest Walkthrough
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-25-2004, 06:46 AM
  3. Walkthrough`
    By Programmer2b in forum C Programming
    Replies: 1
    Last Post: 04-25-2002, 08:46 PM