Mark said:
KL said:
Mark P wrote:
[snip]
I don't see why this whould always return true, but it still has a
problem. What happens if the two strings are equal? What (if
anything, *hint*) gets returned?
DOH! It is always the little things.....and I don't get why I
always get a true value, but I do...gonna ponder on that some more
and work on a different return for ....... uh oh....it is a bool, it
can only return true or false. So I can't test for strings that are
equal....or can I?
As to why you always get "true", it would be helpful if you'd post a
complete, compiling section of code that exhibits the problem.
As to other issue I mentioned, it's not that there's no proper return
value. If two strings are equal, it's usually sensible for < to
return false since the one is *not* less than the other. The problem
in your case is that nothing gets returned because the return
statement is never executed.
class STRING {
char letters[100];
int length;
public:
STRING( ){
length = 0;
for (int a=0; a<100; a++)
letters[a] = ' ';
}
STRING(int num);
STRING(string s){
int a;
for (a=0; a<100; a++)
letters[a] = ' ';
a = 0;
length = s.length();
while (a < length) {
letters[a] = s.at(a);
a++;
}
};
bool operator<(const STRING& b) const{
for (int a=0;a<100;a++){
if( letters[a] != b.letters[a]){
return (letters[a]<b.letters[a]);
}
}
};
friend STRING operator+(const STRING&, const STRING&);
friend int substring(const STRING&, const STRING&);
friend STRING insert(const STRING&, const STRING&, int n);
friend STRING delet(const STRING&, int n, int x);
friend ostream& operator<<(ostream&, const STRING&);
friend istream& operator>>(istream&, STRING&);
};
ostream& operator<< (ostream& os, const STRING& str) {
int a = 0;
while (a < str.length)
os << str.letters[a++];
return os;
}
istream& operator>> (istream& is, STRING& str) {
string s;
getline(is, s);
str.length = s.length();
if (str.length > 100) str.length = 100;
for (int a=0; a<str.length; a++)
str.letters[a] = s.at(a);
return is;
}
STRING operator+(const STRING& a, const STRING& b) {
STRING c,temp_a;
int index=a.length;
int result_length;
int temp_b;
temp_a=a;
result_length=a.length + b.length;
if (result_length > 100){
result_length = 100;
}
temp_b=0;
while (index < result_length){
temp_a.letters[index] = b.letters[temp_b];
temp_b+=1;
index+=1;
}
temp_a.length=result_length;
c=temp_a;
return c;
}
int substring (const STRING& source, const STRING& search){
int num = search.length, pt=0, h=0, g=0,index=0,x;
STRING temp;
while (h <= (source.length - num )) {
index=0;
while (index < search.length){
temp.letters[index] = source.letters[h+index];
index++;
}
h++;
x=0;
g=0;
while (g < num){
if (temp.letters[g] == search.letters[g]){
x++;
g++;
}
else
g=num;
}
if (x==num)
{
pt=h-1;
return pt;
}
}
return -1;
}
STRING insert( const STRING& h, const STRING& i, int n){
STRING temps;
int stringsizes=h.length + i.length, k = 0, x=0 ,l = 0, z =0, j=0,
il=i.length;
if (n>h.length)
stringsizes=stringsizes+n-h.length;
if (stringsizes>100){
stringsizes=100;
}
temps.length=stringsizes;
while (j< n){
temps.letters[j]=h.letters[j];
j++;
}
while (x<il){
temps.letters[j]=i.letters[x];
j++;
x++;
}
while (j<stringsizes){
temps.letters[j] = h.letters[n];
n++;
j++;
}
return temps;
}
STRING delet(const STRING& a, int n, int x){
int count=0, d=0;
STRING f;
f.length=a.length;
while (count<n){
f.letters[count]=a.letters[count];
count++;
}
d=(n+x)+1;
while (d<100){
f.letters[count]=a.letters[d];
count++;
d++;
}
return f;
}
int main( ) {
STRING a, b;
bool n;
string c;
cout << "Enter a string: ";
cin >> a;
cout << "Enter a second string: ";
cin >> b;
n = a<b;
if (n=true){
c="The first string is less than the second string";
}
else {
c="The second string is less that the first string";
}
cout << endl<<n << endl;
cout << c << endl;
return 0;
}
OK this is what I got so far. It is still giving me true every time. I
thought I had that logic ok. I open the for loop, then the if != and return
true if a<b, otherwise I close the if loop and continue the for loop. Am I
totally lost? Do I possibly need a return after the end of the for loop?
Shouldn't it return false if a>b?