Why does the result differ?

My code doesn't seem coincident whenever I run it.

More specifically,

when I enter like <qwer,qwer,qwer,qwer,qwer>, then it works.
However, it doesn't work for <apple, banana, kiwi, melon, orange>.

Can you figure it out?

Here's my code.

FYI, I use the dev c++ complier.
=================================================================
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
#include <iostream>
#include <cstring>

using namespace std;

class MyString{
	
	private:
		char* str;
		int len;
		
	public:
		MyString(); 
		MyString(const char* cp);
		
		MyString &operator=(const MyString &br);
		MyString operator+(const MyString &br);
		bool operator>(const MyString &br);
		void operator<<(ostream &os);
		void operator>>(istream &is);
		
};

MyString::MyString()
{
	str = NULL; len=0;
}
MyString::MyString(const char* cp)
{
	str = new char[strlen(cp)+1];
	strcpy(str, cp);
	len = strlen(cp);
}

MyString &MyString::operator=(const MyString &br)
{
	str = new char[strlen(br.str)+1];
	strcpy(str, br.str);
	len = br.len;
	
	return *this;
}

MyString MyString::operator+(const MyString &br)
{
	MyString temp(str);
	
	temp.len+=br.len;	
	strcat(temp.str, br.str);
		
	return temp;
}

bool MyString::operator>(const MyString &br)
{
	if(len > br.len) return true;
	else return false;
} 

void MyString::operator<<(ostream &os)
{
	os << str << "(" << len << ")" ;
}

void MyString::operator>>(istream &is)
{
	char tmp[80];
	is.getline(tmp, sizeof(tmp));
	*this= MyString(tmp);
}

// general fucntions
 
ostream &operator<<(ostream &os, MyString &br)
{
	br.operator<<(os);
	return os;
}

istream &operator>>(istream &is, MyString &br)
{
	br.operator>>(is);
	return is;
}


int main()
{
	
	MyString ary[5];
	MyString res;
	
	int i;
	cout << "input five kinds of fruits" << endl; 
	for(i=0;i<5;i++)
	{
		cin >> ary[i];
	}
	
	cout << "longer name is : " ;
	if(ary[0] > ary[1]){cout << ary[0] << endl;}
	else cout  << ary[1] << endl;

	
	res = ary[0]+ary[1]+ary[2]+ary[3]+ary[4];
	cout << "print all array : " << res << endl;

	cout << "print all words in the array: " << endl;
	for(i=0;i<5;i++)
	{
		cout << ary[i] << endl;
	}
	
	return 0;
}
Last edited on
Hmm... I don't know why this happens.


A few seconds ago, I ran this code through this site, and it works well as I expected.

So strange thing...

Help me :)
What errors do you get?

Also, you haven't provided a copy constructor. So when you return the MyString object by value, only the respective pointer is copied without memory allocation.

The complier doesn't show me any errors.

However, the "exe" file stops at the 105th line.

Thus, it only shows the longer word and makes some stop message with another window. (I'm sorry for that I couldn't upload the pic)

I'll try to make a copy constructor now. Thx!

>> Oh.. I made one, but it still makes error.
Last edited on
MyString MyString::operator+(const MyString &br) is wrong.
temp contains only a copy of the original string, hence you cannot add a string like on line 38.

What you need is something like this:

char *new_str = new char[strlen(str)+strlen(br.str)+1];

Note that you need to take into account that str/br.str could be nullptr
//coder777

As you told, I changed like this:
1
2
3
4
5
	char* new_str=new char[strlen(str)+strlen(br.str)+1];
	strcpy(new_str, strcat(str, br.str));
	
	return MyString(new_str);
 


But, it's still hard to understand why the previous code didn't work.

Plz see if I understand correctly:
# The former one didn't work because it returns the copy one.
# The latter code worked because it returns the original object of "MyString"

Thx much!
No, the point is the size of the string. It is required that the buffer is large enough to hold the data.

strcpy(new_str, strcat(str, br.str)); is still invalid. strcat(str, br.str) copies br.str beyond the size of str.

Correct is this:
1
2
	strcpy(new_str, str);
	strcat(new_str, br.str); // Important: all copy operations to new_str 


You can make this more effective:
1
2
3
4
5
6
7
8
9
10
11
12
MyString MyString::operator+(const MyString &br)
{
	MyString temp;
	
	temp.len = len + br.len;	
	temp.str = new char[temp.len + 1];

	strcpy(temp.str, str);
	strcat(temp.str, br.str);

	return temp;
}
//code777

Ah, I understood now. The problem is the memory.

It was strange that the copy one cannot be returned since I saw a similar one in my book.

Thx very much. :)
Topic archived. No new replies allowed.