Reputation: 43
I made some of the updates as requested and added the output pic. thanks again everyone
Thanks again everyone!!
I'm sorry in advance to all the moderators out there you're going to hate my question. I've tried to read though some of the examples on here and I'm not getting it. I have a project due tomorrow and these are the overloaded operators I'm having trouble with. The professor says the problem is in my if statements but wont tell me what I'm doing wrong.
bool MyString::operator==(const MyString &right) const
{
if (strlen(m_pString) != strlen(right.m_pString))
{
return false;
}
for (size_t i = 0; i < strlen(m_pString); ++i)
{
if (m_pString[i] == right.m_pString[i])
{
return false;
}
}
return true;
}
bool MyString::operator!=(const MyString &right) const
{
if (strlen(m_pString) != strlen(right.m_pString))
{
return false;
}
for (size_t i = 0; i < strlen(m_pString); ++i)
{
if (m_pString[i] != right.m_pString[i])
{
return false;
}
}
return true;
}
Here are the guidelines for reference:
Overloaded equal relational operator ( operator== )
A == B
The equal relational operator is used to determine if two MyString objects are equal. This should compare each character in both objects m_pString to verify that both null terminated strings are identical. This should return a true if the strings are identical and a false, if not.
Overloaded not equal relational operator ( operator!= )
A != B
The not equal relational operator is used to determine if two MyString objects are not equal. This should compare each character in both objects m_pString. It should return a true immediately if and when a character does not match. This should return a false if the strings are the same.
From what I understand this code should do just that. VB isn't flagging any warning or errors. Thanks you in advance for any input on what's wrong with my IF statements.
This is a picture of the Failed out put from the driver program
Upvotes: 0
Views: 4205
Reputation: 44238
Your function not only incorrect, but ineffective as well, you call strlen()
way too many times:
bool MyString::operator==(const MyString &right) const
{
for (size_t i = 0; true; ++i)
{
if (m_pString[i] != right.m_pString[i] )
return false;
if (m_pString[i] == 0 )
return true;
}
}
Upvotes: 2
Reputation: 172894
1.You're comparing the element of m_pString
with the length of right.m_pString
.
2.The comparison logic seems wrong.
bool MyString::operator==(const MyString &right) const
{
if (strlen(m_pString) != strlen(right.m_pString))
{
return false;
}
for (size_t i = 0; i < strlen(m_pString); ++i)
{
if (m_pString[i] != right.m_pString[i])
// ~~ ~~~~~~~~~~~~~~~~~~
{
return false;
// ~~~~~
}
}
return true;
// ~~~~
}
3.Don't repeat the similar logic.
bool MyString::operator!=(const MyString &right) const
{
return !operator==(right);
}
Upvotes: 5
Reputation: 1412
if (m_pString[i] == strlen(right.m_pString))
should be:
if (m_pString[i] == right.m_pString[i])
You're comparing characters to a string length.
Additionally, in the ==
implementation, you shouldn't be returning true if m_pString[i] == right.m_pString[i]
. Instead, you need to continue checking all the other characters too, but return false if they are !=
.
You have a similar issue in your !=
implementation.
Upvotes: 2