Mystring Problem

In my code I am getting this weird error:
s1 =
s2 = Hello, World
s3 = Welcome to Florida, have a nice day
s4 = 12345

Making the calls:
cin >> s1
getline(cin, s2, ',')
getline(cin, s3)
Enter some sentences: kill
Segmentation fault
vthomas@linprog4.cs.fsu.edu:~/Cop3330_Fall/Assign_5> a.out
s1 =
s2 = Hello, World
s3 = Welcome to Florida, have a nice day
s4 = 12345

Making the calls:
cin >> s1
getline(cin, s2, ',')
getline(cin, s3)
Enter some sentences: kill him, now
please do it now

New string values:
s1 = 108
s2 = 119
s3 = 119
---------------------------
result = Food
s1 = Food
s5 = The concatenation of the catapult is a catamaran
s5.indexOf("cat") returns 7
s5.indexOf("dog") returns -1
s5.getLength() = 48
s5[4] = c
s5[10] = e
s5[15] = o
s5[52] = ascii 0
s5.substring(10,16) = enation of the c
s5.substring(23) = e catapult is a catamara
-----------------------------
words = Greetings, Earthling
words.getLength() = 20
words = Kreepings, Earthzing
words = Kreepings, Earthzing
*** Error in `a.out': free(): invalid next size (fast): 0x00000000013b86e0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7c503)[0x7f32d1137503]
a.out[0x401099]
a.out[0x402503]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f32d10dcb35]
a.out[0x400e49]
======= Memory map: ========
00400000-00404000 r-xp 00000000 00:2c 6881316 /home/majors/vthomas/Cop3330_Fall/Assign_5/a.out
00603000-00604000 r--p 00003000 00:2c 6881316 /home/majors/vthomas/Cop3330_Fall/Assign_5/a.out
00604000-00605000 rw-p 00004000 00:2c 6881316 /home/majors/vthomas/Cop3330_Fall/Assign_5/a.out
013b8000-013d9000 rw-p 00000000 00:00 0 [heap]
7f32cc000000-7f32cc021000 rw-p 00000000 00:00 0
7f32cc021000-7f32d0000000 ---p 00000000 00:00 0
7f32d10bb000-7f32d1271000 r-xp 00000000 08:05 1708178 /usr/lib64/libc-2.17.so
7f32d1271000-7f32d1471000 ---p 001b6000 08:05 1708178 /usr/lib64/libc-2.17.so
7f32d1471000-7f32d1475000 r--p 001b6000 08:05 1708178 /usr/lib64/libc-2.17.so
7f32d1475000-7f32d1477000 rw-p 001ba000 08:05 1708178 /usr/lib64/libc-2.17.so
7f32d1477000-7f32d147c000 rw-p 00000000 00:00 0
7f32d147c000-7f32d1491000 r-xp 00000000 08:05 1727311 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f32d1491000-7f32d1690000 ---p 00015000 08:05 1727311 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f32d1690000-7f32d1691000 r--p 00014000 08:05 1727311 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f32d1691000-7f32d1692000 rw-p 00015000 08:05 1727311 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f32d1692000-7f32d1792000 r-xp 00000000 08:05 1708188 /usr/lib64/libm-2.17.so
7f32d1792000-7f32d1992000 ---p 00100000 08:05 1708188 /usr/lib64/libm-2.17.so
7f32d1992000-7f32d1993000 r--p 00100000 08:05 1708188 /usr/lib64/libm-2.17.so
7f32d1993000-7f32d1994000 rw-p 00101000 08:05 1708188 /usr/lib64/libm-2.17.so
7f32d1994000-7f32d1a7d000 r-xp 00000000 08:05 1708228 /usr/lib64/libstdc++.so.6.0.19
7f32d1a7d000-7f32d1c7d000 ---p 000e9000 08:05 1708228 /usr/lib64/libstdc++.so.6.0.19
7f32d1c7d000-7f32d1c86000 r--p 000e9000 08:05 1708228 /usr/lib64/libstdc++.so.6.0.19
7f32d1c86000-7f32d1c88000 rw-p 000f2000 08:05 1708228 /usr/lib64/libstdc++.so.6.0.19
7f32d1c88000-7f32d1c9d000 rw-p 00000000 00:00 0
7f32d1c9d000-7f32d1cbd000 r-xp 00000000 08:05 1708168 /usr/lib64/ld-2.17.so
7f32d1e89000-7f32d1e8e000 rw-p 00000000 00:00 0
7f32d1eb8000-7f32d1ebc000 rw-p 00000000 00:00 0
7f32d1ebc000-7f32d1ebd000 r--p 0001f000 08:05 1708168 /usr/lib64/ld-2.17.so
7f32d1ebd000-7f32d1ebe000 rw-p 00020000 08:05 1708168 /usr/lib64/ld-2.17.so
7f32d1ebe000-7f32d1ebf000 rw-p 00000000 00:00 0
7fff97404000-7fff97425000 rw-p 00000000 00:00 0 [stack]
7fff974a9000-7fff974ab000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abort
Header:
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
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;

class MyString
{
  friend ostream& operator<< (ostream& , const MyString& );
  friend istream& operator>> (istream& , MyString& );
  friend istream& getline (istream& , MyString& , char delim = '\n');

  friend MyString operator+ (const MyString& , const MyString& );

  friend bool operator< (const MyString& , const MyString& );
  friend bool operator> (const MyString& , const MyString& );
  friend bool operator<=(const MyString& , const MyString& );
  friend bool operator>=(const MyString& , const MyString& );
  friend bool operator==(const MyString& , const MyString& );
  friend bool operator!=(const MyString& , const MyString& );

public:
  MyString();				// empty string
  MyString(const char* );		// conversion from c-string
  MyString(int );			// conversion from int
  ~MyString();				// destructor
  MyString(const MyString& );		// copy constructor
  MyString& operator=(const MyString& );   // assignment operator

  MyString& operator+=(const MyString& );  // concatenation/assignment

  // bracket operators to access char positions
  char& operator[] (unsigned int index);
  const char& operator[] (unsigned int index) const;

  // insert s into the string at position "index"
  MyString& insert(unsigned int index, const MyString& s);

  // find index of the first occurrence of s inside the string
  //  return the index, or -1 if not found
  int indexOf(const MyString& s) const;
  
  int getLength() const;		// return string length
  const char* getCString() const;	// return c-string equiv

  MyString substring(unsigned int , unsigned int ) const;
  MyString substring(unsigned int ) const;

private:

char * str;
int size;


};
#endif
.cpp:
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <iostream>
#include <cstring>
#include <cctype>
#include <iomanip>
#include "mystring.h"

MyString::MyString()
{
	size = 0;
	
	str = NULL;
}                           

MyString::MyString(const char* s)
{
	
	size = strlen(s);
	str = new char[size+1];
	strcpy(str,s);
}               

MyString::MyString(int convert)
{
	int con = convert;
	int counter = 0;

	do	
	{
		con = con / 10;
		counter++;
	}while(con != 0);
	
	size = counter + 1;
	str = new char[size];
 	
	do	
	{
		str[counter - 1] = char((convert % 10) + 48);
		convert = convert / 10;
		counter--;
	}while(counter >= 1);

}                       

MyString::~MyString()
{
	delete [] str;
}                          

MyString::MyString(const MyString& second)
{
	
	size = second.size;
	str = new char[size+1];
	strcpy(str,second.str);

}

MyString& MyString::operator=(const MyString& second )
{
        
	if (this != &second)
        {
                delete []str;
		size = strlen(second.str);
                str = new char[size+1];
                strcpy(str,second.str);
		
        }
	
                return *this;
}

MyString& MyString::operator+=(const MyString& first)
{
   	size = size + first.size;
   	char  *temp = str;
   	str = new char[size+1];
   	strcat(str,first.str);
   	return  *this;
}

ostream& operator<< (ostream& os, const MyString& s)
{
	for (int i = 0; i<s.size;i++)  
	os.put(s.str[i]);
	return os;
}

istream& operator>> (istream& is, MyString& s)
{

    	char c;

	while (is && isspace(is.peek()))
		is.ignore();

	s = " "; // Set the target to the empty string.
	while (is && !isspace(is.peek()))
	{
		is >> c;
		s += c;
	}

    return is;
}


istream& getline (istream& is, MyString& s, char delim)
{
	MyString String;
	char ch;
	is.get(ch);
	while (ch!= '\n' && !is.eof()) 
	{
		String+=ch;
		is.get(ch);
	}
	s=String;
	return is;		
}

MyString operator+ (const MyString& first, const MyString& second)
{
	MyString s = first;
        s += second;
   	return  s; 
}

char& MyString::operator[] (unsigned int index)
{
	
	return str[index];
}

const char& MyString::operator[] (unsigned int index) const
{

	return str[index];
}

MyString& MyString::insert(unsigned int index, const MyString& s)
{
	if(index <= size)	
	{
		char* string = new char[size + s.size];
		
		for(int i = 0; i < index; i++)
			string[i] = str[i];

		for(int i = 0; i < s.size - 1; i++)
			string[i + index] = s.str[i];

		for(int i = index + s.size - 1; i < size + s.size -2; i++)
			string[i] = str[i - s.size +1];

		string[size + s.size -2] = '\0';
		delete [] str;
		str = string;
		size = size + s.size - 1;
	}
	else				
	{
		char* string = new char[size + s.size - 1];

		for(int i = 0; i < size - 1; i++)
			string[i] = str[i];

		for(int i = 0; i < s.size - 1; i++)
			string[size - 1 + i] = s.str[i];

		string[size + s.size - 2] = '\0';
		delete [] str;
		str = string;
		size = size + s.size - 1;
	}
		return *this;  
}


int MyString::indexOf(const MyString& s) const
{
	if(size < s.size)	
		return -1;
	else if(size == s.size)	
	{
		int i = 0;

		while((str[i] == s.str[i]) && (str[i] != '\0'))
			i++;

		if(str[i] == '\0')
			return 0;
		else
			return -1;
	}
	else					
	{
		int index = 0;

		while(index < (size - s.size + 1))
		{	
			int j = 0;
			int k = 0;
			while((s[0] != str[index]) && (index < (size - s.size + 1)))
				index++;
		
			if(index == size - s.size + 1)
				return -1;
			else
			{
				k = index;
				while((s[j] == str[k]) && (s[j] != '\0'))
				{
					k++;
					j++;
				}

				if(s[j] == '\0')
					return index;
				else
					index++;
			}
		}
	}	
}

int MyString::getLength() const
{
	return size;
}

const char* MyString::getCString() const
{
	return str;
}

MyString MyString::substring(unsigned int index, unsigned int sz ) const
{
	MyString sub = substring(index);
	
	if(sub.size - 1 <= sz)
	{
		return sub;
	}
	else
	{
		char* tempstring = new char[sz + 1];
		
		for(int i = 0; i < sz; i++)
		{
			tempstring[i] = sub.str[i];
		}
		
		tempstring[sz] = '\0';
		delete [] sub.str;
		sub.str = tempstring;
		sub.size = sz + 1;
		return sub;
	}
}

MyString MyString::substring(unsigned int index) const
{
	MyString sub;
	int tempsize = size - index;
	
	if(tempsize <= 1)
	{
		sub.str = '\0';
		sub.size = 1;
	}
	else
	{
		char* tempstring = new char[tempsize];
		
		for(int i = 0; i < tempsize - 1; i++)
		{
			tempstring[i] = str[i + index];
		}
		
		tempstring[tempsize - 1] = '\0';	
		delete [] sub.str;
		sub.str = tempstring;
		sub.size = tempsize;
	}
	return sub;
}


bool operator< (const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)<0;
}

bool operator> (const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)>0;
}

bool operator<=(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)<=0;
}

bool operator>=(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)>=0;
}

bool operator==(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)==0;
}

bool operator!=(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)!=0;
}
Driver:
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
#include <iostream>
using namespace std;

#include "mystring.h"

int main()
{
   MyString s1;
   MyString s2("Hello, World");
   MyString s3 = "Welcome to Florida, have a nice day";
   MyString s4 = 12345;

   cout << "s1 = " << s1 << '\n';
   cout << "s2 = " << s2 << '\n';
   cout << "s3 = " << s3 << '\n';
   cout << "s4 = " << s4 << '\n';
   cout << '\n';

   cout << "Making the calls:\n"
        << "\tcin >> s1\n"
        << "\tgetline(cin, s2, ',')\n"
        << "\tgetline(cin, s3)\n";
   cout << "Enter some sentences: ";
  
   cin >> s1;
   getline(cin,s2,',');
   getline(cin,s3);

   cout << "\nNew string values:\n";

   cout << "s1 = " << s1 << '\n';
   cout << "s2 = " << s2 << '\n';
   cout << "s3 = " << s3 << '\n';
   cout << "---------------------------\n";

// ----------------------------------

   s1 = "Dog";
   s2 = "Food";
   MyString result = s1 + s2;
   cout << "result = " << result << '\n';

   s1 += s2;
   cout << "s1 = " << s1 << endl;

   const MyString s5 = "The concatenation of the catapult is a catamaran";
   cout << "s5 = " << s5 << endl;
   cout << "s5.indexOf(\"cat\") returns " << s5.indexOf("cat") << '\n';
   cout << "s5.indexOf(\"dog\") returns " << s5.indexOf("dog") << '\n';

   cout << "s5.getLength() = " << s5.getLength() << '\n';

   cout << "s5[4] = " << s5[4] << '\n';
   cout << "s5[10] = " << s5[10] << '\n';
   cout << "s5[15] = " << s5[15] << '\n';
   cout << "s5[52] = ascii " << static_cast<int>(s5[52]) << '\n';

   cout << "s5.substring(10,16) = " << s5.substring(10,16) << '\n';
   cout << "s5.substring(23) = " << s5.substring(23) << '\n';

   cout << "-----------------------------\n";

   MyString words = "Greetings, Earthling";

   cout << "words = " << words << '\n';
   cout << "words.getLength() = " << words.getLength() << '\n';
   words[0] = 'K';
   words[4] = 'p';
   words[16] = 'z';

   cout << "words = " << words << '\n';

   words[25] = 'Q';
   cout << "words = " << words << '\n';

   words.insert(11, "Insane ");
   cout << "words = " << words << '\n';

   cout << "-----------------------------\n";

   MyString x = "apple", y = "apply";
   cout << "x = " << x << '\n';
   cout << "y = " << y << '\n';

   if (x < y)		cout << "x < y is true\n";
   if (x > y)		cout << "x > y is true\n";
   if (x <= y)		cout << "x <= y is true\n";
   if (x >= y)		cout << "x >= y is true\n";
   if (x == y)		cout << "x == y is true\n";
   if (x != y)		cout << "x != y is true\n";



}
Topic archived. No new replies allowed.