I assume you've deliberately not allocated any space for a char array in your stringTokenizer class, and are using stringTokenizer as if it were a Functor class?
your next() method never seems to modify the originalString pointer, so whenever its called, all it does is return a pointer to the beginning of the string.
CODE
char* stringTokenizer::next(){
for(int x = 0; x < sizeof(originalString); x++)
{
if(originalString[x] == ' ')
{
originalString[x] = token;
}
}
return originalString;
}
You also have a problem here
for(int x = 0; x < sizeof(originalString); x++) sizeof(originalString) is the size of a pointer in bytes. if you want the length of the string, use strlen() from the <cstring> libary.
In any case - the condition for this while loop will never change
CODE
tokens = firstObject.next();
while (tokens != NULL)
{
cout << tokens << endl;
tokens = firstObject.next();
}
Presumably, you want something like this..
CODE
char* stringTokenizer::next()
{
//boolean - Has the delimiter been found?
bool next_token = false;
//Run while not at the null-terminator
while( originalString[0] != '\0' )
{
if( originalString[0] == token )
//delimiter found!
next_token = true;
//Move the string pointer forward 1 place
++originalString;
if( next_token == true )
return originalString;
}
//No delimiter found - reached end of the string
return NULL;
}
Also, this line shouldn't compile - size needs to be const unless you're dynamically allocating your array
CODE
int size = 256;
char sentence[size];
either
const int size = 256; or
enum { size = 256 }; should fix it.
This post has been edited by Bench: 5 Sep, 2007 - 02:30 AM