Thread: recycling chars

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    Question 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?
    My Website

    "Circular logic is good because it is."

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I have a question. How can this ever be true?

    if(myString[index] == 0 &&
    myString[index] + 1 >= 128)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    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;
    
    };
    Last edited by DavidP; 02-22-2002 at 08:14 PM.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM