I am writing a string tokenization program for setting homework in C ++ that uses pointers. However, when I run and debug it, it says that my pStart pointer is invalid. I feel that my problem is with my param'ed constructor, I included both the constructor and the creation of the object below.
I would appreciate it if you could tell me why it says pStart is a bad pointer when I debug it.
Thank!
StringTokenizer::StringTokenizer(char* pArray, char d)
{
pStart = pArray;
delim = d;
}
StringTokenizer tk( "A test char array", ' ' );
Full stringtokenizer.cpp file:
#include "stringtokenizer.h"
#include <iostream>
using namespace std;
StringTokenizer::StringTokenizer(void)
{
pStart = NULL;
delim = 'n';
}
StringTokenizer::StringTokenizer(const char* pArray, char d)
{
pStart = pArray;
delim = d;
}
char* StringTokenizer::Next(void)
{
char* pNextWord = NULL;
while (pStart != NULL)
{
if (*pStart == delim)
{
*pStart = '\0';
pStart++;
pNextWord = pStart;
return pNextWord;
}
else
{
pStart++;
}
}
return pNextWord;
}
The Next supossed function returns a pointer to the next word in the char array. It is not completed at present. :)
Full stringtokenizer.h:
#pragma once
class StringTokenizer
{
public:
StringTokenizer(void);
StringTokenizer(const char*, char);
char* Next(void);
~StringTokenizer(void);
private:
char* pStart;
char delim;
};
Full main.cpp file:
const int CHAR_ARRAY_CAPACITY = 128;
const int CHAR_ARRAY_CAPCITY_MINUS_ONE = 127;
char words[CHAR_ARRAY_CAPACITY];
char* nextWord;
cout << "\nString Tokenizer Project";
cout << "\nyour name\n\n";
cout << "Enter in a short string of words:";
cin.getline ( words, CHAR_ARRAY_CAPCITY_MINUS_ONE );
StringTokenizer tk( words, ' ' );
while ( ( nextWord = tk.Next ( ) ) != NULL )
{
cout << nextWord << endl;
}
system("PAUSE");
return 0;