Thread: Some error somewhere!

  1. #1
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Some error somewhere!

    String.h file:
    Code:
    #ifndef _Mystring
    #define _Mystring
    
    #include <iostream>
    
    class String;
    istream& operator >> ( istream&, String& );
    ostream& operator << ( ostream&, const String& );
    
    class String
    {
     public:
      String();
      String( const char *);
      String( const String& );
    
      ~String();
    
      //overloaded set of operators
      String& operator = (const String&);
      String& operator = (const char*);
    
      bool operator == (const String&);
      bool operator == (const char *);
    
      char& operator [] (int);
    
      //member functions
      int size() { return _size;}
      char * c_str() { return _string;} 
    
      private:
      int _size;
      char *_string;
    };
    
    
    #endif
    String.cpp file:
    Code:
    #include "String.h"
    #include <cstring>
    
    
    
    inline bool String::operator == (const String& rhs)
    {
      if ( _size != rhs._size )
        return false;
      return strcmp(_string, rhs._string) ? false : true;
    }
    
    inline bool String::operator == (const char *s)
    {
      return strcmp(_string, s) ? false : true;
    }
    
    inline String::String ()
    {
      _size = 0;
      _string = 0;
    }
    
    inline String::String (const char *str)
    {
      if (!str)
        {
          _size = 0;
          _string = 0;
        }
      else
        {
          _size = strlen(str);
          _string = new char [_size + 1];
          strcpy(_string, str);
        }
    }
    
    inline String::String (const String &rhs)
    {
      _size = rhs._size;
      if (! rhs._string)
        _string = 0;
      else
        {
          _string = new char [_size + 1];
          strcpy(_string, rhs._string);
        }
    }
    
    inline String::~String() { delete [] _string; }
    
    
    inline String& String::operator = (const char *s)
    {
      if ( !s )
        {
          _size = 0;
          delete [] _string;
          _string = 0;
        }
      else
        {
          _size = strlen( s );
          delete [] _string;
          _string = new char [ _size + 1 ];
          strcpy( _string, s);
        }
      return *this;
    }
    
    inline String& String::operator = (const String& rhs )
    {
      if ( this != &rhs )
        {
          delete [] _string;
          _size = rhs._size;
    
          if ( ! rhs._string )
    	_string = 0;
          else
    	{
    	  _string = new char [ _size + 1 ];
    	  strcpy( _string, rhs._string );
    	}
        }
      return *this;
    }
    
    #include<cassert>
    
    inline char& String::operator [] ( int elem )
    {
      assert( elem >= 0 && elem < _size );
      return _string [ elem ];
    }
    
    #include<iomanip>
    
    inline istream& operator >> (istream &io, String& s)
    {
      const int limit_string_size = 4096;
      char inBuf[ limit_string_size ];
    
      io >> setw (limit_string_size) >> inBuf;
      s = inBuf;
      return io;
    }
    
    inline ostream& operator << (ostream& os, const String& s)
    {
      return os << s.c_str();
    }
    main.cpp file:
    Code:
    #include <iostream>
    #include "String.h"
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    
    int main()
    {
      int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,
        theCnt = 0, itCnt = 0, wdCnt = 0, notVowel = 0;
    
      String buf, the("the"), it("it");
    
      while(cin >> buf )
        {
          ++wdCnt;
    
          cout << buf << ' ';
    
          if ( wdCnt % 12 == 0 )
    	cout << endl;
    
          if ( buf == the || buf == "the" )
    	++theCnt;
          else
    	if (buf == it || buf == "it" )
    	  ++itCnt;
    
          for ( int ix = 0; ix < buf.size(); ++ix )
    	{
    	  switch (buf[ix])
    	    {
    	    case 'a' : case 'A' : ++aCnt; break;
    	    case 'e' : case 'E' : ++eCnt; break;
    	    case 'i' : case 'I' : ++iCnt; break;
    	    case 'o' : case 'O' : ++oCnt; break;
    	    case 'u' : case 'U' : ++uCnt; break;
    	    default : ++notVowel; break;
    	    }
    	}
        }
    
      cout << "\n\n"
           << "Words read: " << wdCnt << "\n\n"
           << "the/The: " << theCnt << '\n'
           << "it/It: " << itCnt << "\n\n"
           << "non-vowels read: " << notVowel << "\n\n"
           << "a: " << aCnt << '\n'
           << "e: " << eCnt << '\n'
           << "i: " << iCnt << '\n'
           << "o: " << oCnt << '\n'
           << "u: " << uCnt << '\n';
    }
    I'm having some problems getting this to compile. I don't think there are any large errors, but can someone spot the problem and let me know?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm having some problems getting this to compile. I don't think there are any large errors, but can someone spot the problem and let me know?
    I tend to find the compiler is good at spotting errors... if you really want help, why don't you post the errors your compiler is giving you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed