problems with >> istream overloading

This is a discussion on problems with >> istream overloading within the C++ Programming forums, part of the General Programming Boards category; Though I managed to get the program too compile I still have a runtime error. The program crashes after I ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    problems with overloading

    Though I managed to get the program too compile I still have a runtime error. The program crashes after I enter my name.

    here is stringm.cpp
    Code:
    #include <stdlib.h>
    #include "stringh.hpp"
    
    int main()
    {
        String s1(" and I am a C++ student.");
        String s2="Please enter your name: ";
        String s3;
        cout << s2;
        cin >> s3;
        s2="My name is "+s3;
        cout << s2 << ".\n";
        s2=s2+s1; // overloaded =, + operators
        s2.stringup();// convert to upper case
        cout << "The string\n" << s2 << "\ncontains" <<s2.has('A')
             << " 'A' chachters in it.\n";
        s1="red";  
        String rgb[3]={String(s1), String("green"), String("blue")};
        cout << "Enter the name of a primary color for mixing light: ";
        String ans;
        bool success=false;
        while (cin>>ans)
        {
              ans.stringlow();  //convert to lower case
              for (int i=0;i<3;i++)
              {
                  if (ans==rgb[i])
                  {
                     cout << "That's right!\n";
                     success=true;
                     break;
                  }
              }
              if (success)
                 break;
              else
                  cout << "Try again!\n";
        }
        cout << "Bye!\n";
        system("PAUSE");
        return 0;
    }
    here is the header
    Code:
    //string prblm 2 pg 537 C++ Primer plus Third Edition
    #ifndef _STRINGH_HPP_
    #define _STRINGH_HPP_
    #include <iostream>
    using namespace std;
    
    class String
    {
          private:
                  char * str;
                  int len;
          public:
                 // constructor
                 String(const char * s);
                 String();
                 // copy constructor
                 String(const String & st);
                 //destructor
                 ~String();
                 // inline function
                 int length() const {return len;}
                 
                 void stringup(); //{str=strupr(str);} string.h
                 void stringlow(); //{str=strlwr(str);}
                 
                 int has(char s);// count chars 
                 // overloaded operators
                 String & operator=(const String & st);  
                 String & operator=(const char * s);
                 //friend functions
                 friend String operator+(String & str1,const String &str2);
                 friend String operator+(char * s,String &str2);
                 friend bool operator>(const String &str1,const String &str2);
                 friend bool operator<(const String &str1,const String &str2);
                 friend bool operator==(const String &str1,const String &str2);
                 friend ostream & operator<<(ostream & os, const String & st);
                 friend istream & operator>>(istream & is, String & st);
    };
    #endif
    here is the function definitons
    Code:
    //string prblm 2 pg 537 C++ Primer plus Third Edition
    #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
    #include "stringh.hpp"
    
    //class methods
    String::String(const char*s) // make String from c string
    {
           len=strlen(s);
           str=new char[len+1];
           strcpy(str,s);
    }
    
    String::String()
    {
           len=0;
           str = new char[1];
           str[0]='\0';
    }
    
    String::String(const String & st) // copy constructor
    {
           len=st.len;
           str=new char[len+1];
           strcpy(str,st.str);
    }
    
    String::~String()
    {
           delete [] str;
    }
    void String::stringup()
    {
         for ( int x = 0; x < strlen (str); x++ )
               str[x] = toupper ( str[x] );
    }
    
    void String::stringlow()
    {
         for ( int x = 0; x < strlen (str); x++ )
               str[x] = tolower ( str[x] );
    }
    int String::has(char s)
    {
        int c=0;
        for (int i=0; i<strlen(str); i++)
            if (str[i]==s)
               c++;
        return c;
    }
    
    // assign String to a String
    String & String::operator=(const String & st)
    {
           if (this==&st)
              return *this;
           delete [] str;
           len=st.len;
           str=new char[len+1];
           strcpy(str,st.str);
           return *this;
    }
    
    // assign a C string to a String
    String & String::operator=(const char * s)
    {
           delete [] str;
           len = strlen(s);
           str=new char[len+1];
           strcpy(str,s);
           return *this;
    }
    
    //concatinate two strings
    String operator+(String &str1,const String &str2)
    {      
           int size=str1.len+str2.len+1;
           char * temp=new char[str1.len+1];
           strcpy(temp,str1.str);
           //resize str1.str to hold concatenate string
           delete [] str1.str;
           str1.str=new char[size];
           strcpy(str1.str,temp);
           delete [] temp;
           strcat(str1.str,str2.str);
           return str1;
    }
    
    String operator+(char * s,String &str2)
    {
           int size=str2.len+strlen(s)+1;
           char * temp=new char[str2.len+1];
           strcpy(temp,str2.str);
           //resize str1.str to hold concatenate string
           delete [] str2.str;
           str2.str=new char[size];
           strcpy(str2.str,temp);
           delete [] temp;
           str2.str=strcat(s,str2.str);
           return str2;
    }
    
    // true if st1 preceds st2 in collating sequence
    bool operator<(const String &st1, const String &st2)
    {
         if (strcmp(st1.str,st2.str)<0)
            return true;
         else 
              return false;
    }
    
    // true if st1 same as st2 
    bool operator==(const String &st1, const String &st2)
    {
         if (strcmp(st1.str,st2.str)==0)
            return true;
         else 
              return false;
    }
    
    //display String
    ostream & operator<<(ostream & os,const String & st)
    {
            os << st.str;
            return os;
    }
    
    istream & operator>>(istream & is, String & st)
    {
            char temp[80];
            is.get(temp,80);
            if (is)
               st=temp;
            while (is && is.get() != '\n')
                  continue;
            return is;
    }
    I know this is a lot of code I suspect there is an error in the 2nd overloaded + operator used to concat the strings please help.
    Last edited by rip1968; 05-06-2002 at 02:27 PM.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    double posting will not help you. please delete this thread.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I posted on the C board by accident.
    It has been moved here.

    I manage to get my program to compile I will edit my post with the complete source because my prog crashes right after I enter the name. Hopefully someone can help.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    update 2 second operator overloding+

    here is another update to the second operator+ function
    Code:
    String operator+(char * s,String &str2)
    {
           int size=str2.len+strlen(s)+1;
           char * temp=new char[str2.len+1];
           strcpy(temp,str2.str);
           //resize str2.str to hold concatenate string
           delete [] str2.str;
           str2.str=new char[size];
           str2.str=strcat(s,temp);
           delete [] temp;
           return str2;
    }
    I guess i should edit the subject of this post too.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I managed to correct the problem on my own, wich is usually more rewarding
    here is a simple solution for the second operator+
    maybe this will help someone else
    Code:
    String operator+(char * s,String &str2)
    {      
           String temp(s);
           str2=temp+str2; uses previous overloaded + operator      
           return str2;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading >>
    By sentienttoaster in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2003, 06:19 PM
  2. overloading >> operator
    By Diamonds in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2003, 01:01 AM
  3. istream >> overloading
    By wazza13 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 10:56 PM
  4. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 03:07 PM

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