-
recycling chars
I am making this class:
Code:
class STRING_DTP
{
public:
STRING_DTP ( ) { myString.resize(4);
for(int x = 0; x < 4; x++) myString[x] = 0; }
STRING_DTP ( char *str ) { myString = string(str); }
STRING_DTP ( string str ) { myString = str; }
operator ++ ( )
{
string temp;
int index = myString.length() - 1;
while(index > -1 || myString[index] + 1 >= 128)
index--;
if(myString[index] == 0 &&
myString[index] + 1 >= 128)
{
temp = myString;
myString.resize(myString.length()+1);
myString.replace(1, myString.length(), temp);
myString[0] = 0;
}
else myString[index] += 1;
}
operator ++ ( int )
{
string temp;
int index = myString.length() - 1;
while(index > 0 || myString[index] + 1 == 127)
index--;
if(myString[index] == 0 &&
myString[index] + 1 >= 128)
{
temp = myString;
myString.resize(myString.length()+1);
myString.replace(1, myString.length(), temp);
myString[0] = 0;
}
else myString[index] += 1;
}
string myString;
};
the string data type is from cstring.h, included in Borland C++. It is their standard string class. If you dont have Borland, it is not that hard to see what it is doing. If you have Borland, you can actually try compiling code using this class.
Anyways, there is a problem in it that I am trying to get out, but cant figure out how. This string class works a bit different then others. It works like an int. Lets say you have this string:
"z"
Now, contrary to ALL you know about the char 'z', assume it has an ASCII value of 127, even though it really doesnt. Just assume it does. Also assume that 'a' has an ASCII value of 1, and '0' has an ASCII value of 0.
When i use the ++ operator in this string, it should result to this (assuming the things i told you, it wont look like this in actuallity):
"a0"
Because in a normal char data type, the range goes from -128 to 128. Therefore when it reaches 128 it recycles to 0, and adds 1 to the next place value.
Thats how I am trying to make this string class work. However, there is a problem when it recycles. It gives me an abnormal program termination. Anyone see a problem?
-
I have a question. How can this ever be true?
if(myString[index] == 0 &&
myString[index] + 1 >= 128)
-
>I have a question. How can this ever be true?
No, if myString[index] is 0 then 0 + 1 certainly will not be 128. This would make more sense if it were
if(myString[index] == 0 && myString[index+1] >= 128)
-Prelude
-
sorry, i posted old code. here is the most recent:
Code:
class STRING_DTP
{
public:
STRING_DTP ( ) { myString.resize(4);
for(int x = 0; x < 4; x++) myString[x] = 0; }
STRING_DTP ( char *str ) { myString = string(str); }
STRING_DTP ( string str ) { myString = str; }
operator ++ ( )
{
string temp;
int index = myString.length() - 1;
while(index > 0 || myString[index] + 1 >= 127)
index--;
if((myString[index] == 0) &&
(myString[index] + 1 >= 127))
{
temp = myString;
myString.resize(myString.length()+1);
myString.replace(1, myString.length(), temp);
myString[0] = 1;
myString[myString.length()-1] = 0;
}
else myString[index] += 1;
}
operator ++ ( int )
{
string temp;
int index = myString.length() - 1;
while(index > 0 || myString[index] + 1 >= 127)
index--;
if((myString[index] == 0) &&
(myString[index] + 1 >= 127))
{
temp = myString;
myString.resize(myString.length()+1);
myString.replace(1, myString.length(), temp);
myString[0] = 1;
myString[myString.length()-1] = 0;
}
else myString[index] += 1;
}
string myString;
};