c++ string class concantenate
Mar 14, 2014 at 3:28am UTC
I can't figure out how to write this ConCatenation member function for my Mystring class. I need it to ConCat Mystring s onto the tail end of another Mystring (stored in sval).
The error i get "Control reaches end of non void function"
sval stores the string values
slen stores the string length
Mystring Mystring :: ConCat(Mystring s){
for(int i= 0; s.sval[i] <= s.slen; i++){
sval[slen + 1] = s.sval[i];
}
}
---------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#include <iostream>
using namespace std;
class Mystring{
private :
char sval[100];
int slen;
public :
//constructors
Mystring(): slen(0){}
Mystring(char s[]);
//access functions
bool EqualTo(Mystring s);
bool GreaterThan(Mystring s);
bool LessThan(Mystring s);
int Length();
Mystring ConCat(Mystring s);
//modifier funtions
char & At(int pos);
void Reverse();
void ToLowercase();
void ToUppercase();
//input & output functions
void Read();
void Write();
//tools
bool IsPalindrome();
};
bool go = true ;
int inputcount = 0;
bool IsPalindrome(char s[]);
int main(){
Mystring object[100];
Mystring y_n_object[100];
int inputcount = 0;
//object[inputcount]; //create object? Or is object already created?
//y_n_object[inputcount]; //create object? Or is object already created?
cout << "Do you want to input a String? ('Yes' or 'No')" << ": " ;
y_n_object[inputcount].Read(); // cin.getline(yes_no, MAX2);
y_n_object[inputcount].ToLowercase();
do {
if (y_n_object[inputcount].EqualTo(Mystring("n" )))
go = false ;
else if (y_n_object[inputcount].EqualTo(Mystring("yes" )))
{
cout << "Please Enter String" << ": " ;
object[inputcount].Read();
++inputcount;
cout << "Do you want to input another String (Yes or No)" ;
y_n_object[inputcount].Read();
y_n_object[inputcount].ToLowercase();
}
else {
cout << "Please enter a valid response - Either 'Yes' or 'No':" ;
y_n_object[inputcount].Read();
y_n_object[inputcount].ToLowercase();
}
}
while (go);
cout << endl << "There are a total of " << inputcount << " " << "strings input" ;
cout << endl;
for (int i = 0; i < inputcount; i++){
if (object[i].IsPalindrome()){
object[i].ConCat(Mystring(" Plaindrome" ));
object[i].Write();
cout << endl;
}
else
object[i].Write();
cout << endl;
}
return 0;
}
//string class constructors
Mystring :: Mystring(char s[])
{
for (slen=0; s[slen] && (slen < 100); slen++)
sval[slen] = s[slen];
}
//String class access functions
int Mystring:: Length(){ //Is correct
return slen;
}
bool Mystring:: EqualTo(Mystring s){
if ( slen != s.slen)
return false ;
for (int i = 0; i<slen && sval[i] == s.sval[i]; i++){
if ( i == slen)
return true ;
}
return false ;
}
bool Mystring:: GreaterThan(Mystring s){
for (int i = 0; i < slen; i++){
if (s.sval[i] > sval[i])
return true ;
else if (s.sval[i] < sval[i])
return false ;
}
if (s.sval[slen] > sval[slen])
return true ;
return false ;
}
//if both strings are the same but one is a char longer, watch out for this.
bool Mystring:: LessThan(Mystring s){
for (int i = 0; i < slen; i++){
if (s.sval[i] < sval[i])
return true ;
else if (s.sval[i] > sval[i])
return false ;
}
if (s.sval[slen] < sval[slen])
return true ;
return false ;
} //what is wrong with this?
Mystring Mystring :: ConCat(Mystring s){
for (int i= 0; s.sval[i] <= s.slen; i++){
sval[slen+1] = s.sval[i];
}
} //what is wrong with this?
//modifier functions
char & Mystring :: At(int pos){ //Is Correct
return (pos < slen) ? sval[pos] : sval[slen-1];
}
void Mystring :: ToLowercase(){
for (int i = 0; sval[i] != '\0' ; i++ )
if ((sval[i] >='A' ) && (sval[i] <= 'Z' ))
sval[i] += 32;
}
void Mystring :: ToUppercase(){
for (int i = 0; sval[i] != '\0' ; i++ )
if ((sval[i] >='a' ) && (sval[i] <= 'A' ))
sval[i] -= 32;
}
void Mystring:: Reverse(){
char temp[100];
for (int i = 0; i < slen; i++){
temp[i] = sval[i];
sval[i] = sval[slen];
sval[slen] = temp[i];
slen--;
}
}
//input/output functions
void Mystring:: Read(){
char input_string[101];
cin >> input_string;
for (slen=0; input_string[slen] && (slen<100); slen++)
sval[slen] = input_string[slen];
}
void Mystring:: Write(){
for (int i = 0; i < slen; i++)
cout << sval[i];
}
bool Mystring:: IsPalindrome(){
int leftpos = 0;
int rightpos = slen;
for ( ; sval[leftpos] == sval[rightpos] && leftpos < rightpos; leftpos++, rightpos--)
{
while (sval[leftpos] == ' ' )
++leftpos;
while (sval[rightpos] == ' ' )
--rightpos;
if (sval[leftpos] != sval[rightpos])
return false ;
}
return true ;
}
Mar 14, 2014 at 3:34am UTC
Your ConCat method is missing a return statement. The return type appears to be Mystring.
Mar 14, 2014 at 3:51am UTC
Your function is supposed to return a Mystring. Probably you want to add return *this ;
Topic archived. No new replies allowed.