Thread: Pascal Strings

  1. #1
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49

    Pascal Strings

    Hello, I'm creating a string class similar to the format of pascal string or so I heard, the type where the string would not exceed 255 characters. I'm having a bit of problem getting it to the output, how do I pass the entire array, not including the character count at [0] to ostream?

    Code:
    class PascalString {
    private:
      unsigned char* data;
      
      void copyString(const char* cstr) {
        int num_char = std::strlen(cstr);
        if(num_char > UCHAR_MAX) {
          return; // or abort() where is abort() or exit() in? stdlib.h?
        }
        data = new unsigned char[num_char+1];
        data[0] = num_char;
        unsigned char* copier = data[1];
        for(int i = 0; i < num_char; ++i) {
          *copier = cstr[i];
          ++copier;
        }
        return;
      }
    public:
      PascalString() {
        data = 0;
      }
      
      PascalString(const char* s) {
        copyString(s);
      }
      
      friend std::ostream& operator<<(std::ostream& out, const PascalString& ps);
    };
    
    std::ostream& operator<<(std::ostream& out, const PascalString& ps) {
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >how do I pass the entire array, not including the character count at [0] to ostream?
    Code:
    std::ostream& operator<<(std::ostream& out, const PascalString& ps) {
       for (unsigned int i=1; i<=ps.data[0]; ++i)
          std::cout << ps.data[i];
    }
    Or you could store a null terminator at the end of the array and do it like this:
    Code:
    std::ostream& operator<<(std::ostream& out, const PascalString& ps) {
       std::cout << ps.data+1;
    }

  3. #3
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49
    I want to return a reference to ostream, is there no other way?

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Here is a fixed version of swoopy's first example. (Added return statement and switched std::cout/out.)
    Code:
    std::ostream& operator<<(std::ostream& out, const PascalString& ps) {
       for (unsigned int i=1; i<=ps.data[0]; ++i)
          out << ps.data[i];
    
       return out;
    }
    Quote Originally Posted by Frost Drake
    I want to return a reference to ostream, is there no other way?
    I think you meant "I want to return a reference to an ostream," or "I want to return a reference to 'out'." (Just making sure you're thinking correctly.)
    Last edited by Rashakil Fol; 01-13-2006 at 12:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM