-
Inheritance
What I'm trying to do is pass a string from my derived class to my base class. A function called nextToken() should return a String object up to the delimiter(in this case ':').
This string
dunc0035:!:5701:500::/shome/dunc0035:/bin/ksh
Should be displayed as
dunc0035 ! 5701:500 /shome/dunc0035 /bin/ksh
When I run my program it display some of the right info..and some garbage....
This is the code I have....
Code:
class String
{
protected:
enum { SZ = 100 };
char str[SZ];
public:
String ()
{ str[0] = '\0'; }
String (char s[] )
{ strcpy(str, s); }
void display() const
{ cout << str; }
operator char*()
{ return str; }
};
class StringTokenizer : public String
{
protected:
int i;
char colon;
public:
StringTokenizer(char obj[], char cn ) : i(0), colon(cn)
{
strcpy(str,obj);
}
int countToken(char obj[], char colon)
{
int len = strlen(obj);
i++;
for(int n =0; n< len; n++)
{
if(obj[n] == colon)
i++;
}
return i;
}
bool hasMoreTokens ()
{
int length = strlen(str);
return (i < length) ? true : false;
}
String nextToken()
{
int a = strlen(str);
char temp[80];
int j=0;
while(str[i] != colon)
{
temp[j]=str[i];
j++;
i++;
}
i++;
return String(temp);
}
};
int main()
{
String s; s= "dunc0035:!:5701:500::/shome/dunc0035:/bin/ksh";
StringTokenizer tokens(s, ':');
while ( tokens.hasMoreTokens() )
{
tokens.nextToken();
String token = tokens.nextToken();
cout << static_cast<char*>(token);
cout<< endl;
}
return 0;
}
Any help would be great...thanks
-
Code:
> String nextToken()
> {
> int a = strlen(str);
> char temp[80];
> int j=0;
>
> while(str[i] != colon)
> {
> temp[j]=str[i];
>
> j++;
> i++;
> }
Teminate the string here, so:
Code:
String nextToken()
{
int a = strlen(str);
char temp[80];
int j=0;
while(str[i] != colon)
{
temp[j]=str[i];
j++;
i++;
}
temp[j] = '\0';
-
> tokens.nextToken();
> String token = tokens.nextToken();
And you have two nextToken() calls here, so it will display every other token.
-
Thanks .... it all worked...
-
Ok....now I have to change it to an Array of 10...
Code:
* const int SIZE = 10;
String s[SIZE]= {
{"root:!:0:0::/:/bin/ksh"},
{"daemon:!:1:1::/etc:"},
{"markm:!:253:501:Maitang Mark:/thome/markm:/usr/bin/ksh"},
{"mcmasti:!:332:501:Ian McMaster:/thome/mcmasti:/usr/bin/ksh"},
{"rozo0001:!:5700:500::/shome/rozo0001:/bin/ksh"},
{"dunc0035:!:5701:500::/shome/dunc0035:/bin/ksh"},
{"lapo0045:!:5702:500::/shome/lapo0045:/bin/ksh"},
{"cast0010:!:5703:500::/shome/cast0010:/bin/ksh"},
{"benn0049:!:5704:500::/shome/benn0049:/bin/ksh"},
{"hhhhh:!:5710:500::/shome/ferr0026:/bin/ksh"}};
StringTokenizer tokens(s, ':');
How do I make it work with an Array?????
Thanks again for any help...
-
>How do I make it work with an Array?????
Code:
StringTokenizer tokens[] = {
StringTokenizer(s[0], ':'),
StringTokenizer(s[1], ':'),
StringTokenizer(s[2], ':'),
StringTokenizer(s[3], ':'),
StringTokenizer(s[4], ':'),
StringTokenizer(s[5], ':'),
StringTokenizer(s[6], ':'),
StringTokenizer(s[7], ':'),
StringTokenizer(s[8], ':'),
StringTokenizer(s[9], ':'),
};
:p