Ok, I have searched the archives and cannot find an answer to my question, so ... I am tokenizing strings by the delimiter ':' and cannot seem
to assign and index value from the output of the strcspn function. Anyone help?
Also having trouble comparing a strlen output to an integer value, any clue?
Already spent too many hours trying to figure it out.. Thanks!Code:#include <iostream> #include <cstring> //for strcpy(), strcat() etc. using namespace std; //////////////////////////////////////////////////////////////// class String //user-defined string type { protected: enum { SZ = 80 }; //size of all String objects char str[SZ]; //holds a C-string public: String() //no-arg constructor { str[0] = '\0'; } String( char s[] ) //1-arg constructor { strcpy(str, s); } // convert C-string to String void display() const //display the String { cout << str; } operator char*() //conversion operator { return str; } //convert String to C-string String operator + (String s1); bool operator == (String s1); bool operator > (String s1); bool operator < (String s1); bool operator != (String s1); bool operator >= (String s1); bool operator <= (String s1); }; String String::operator + (String s1) { strcat(str, s1); return s1; } bool String::operator == (String s1) { return (strcmp(str, s1)==0 ) ? true : false; } bool String::operator > (String s1) { return (strcmp(str, s1) > 0 ) ? true : false; } bool String::operator < (String s1) { return (strcmp(str, s1) < 0 ) ? true : false; } bool String::operator != (String s1) { return (strcmp(str, s1) != 0 ) ? true : false; } bool String::operator >= (String s1) { return (strcmp(str, s1) >= 0 ) ? true : false; } bool String::operator <= (String s1) { return (strcmp(str, s1) <= 0 ) ? true : false; } class StringTokenizer : public String { private: int index; char delimiter; public: StringTokenizer(String s1, char d) { d = ' '; } int countToken() { int temp; temp = strcspn( str , ":" ); //<<<<---- Problems here temp++; //account for the end of the string return temp; } bool hasMoreToken() { if (index < strlen(str)) //<<<----- And Here! return true; else return false; } String nextToken() { str.erase(0, index); //erase everything from front of string to index int end = str.find(':'); //find the end of the token int null = str.find('\0'); //find the end of the string str.erase(end, null - 1); //erase everything from end of token to 1 before null } };
Andrew