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
here is the headerCode:#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 function definitonsCode://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
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.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; }



LinkBack URL
About LinkBacks


