2
    int main(void)
{
char *text = (char*)malloc ( 100 *sizeof( char));
cout << "Enter the first arrangement of data." << endl;
cin.getline(text, 100);
char *token = strtok(text, " ");
char *data = (char*)malloc ( 100*sizeof( char));
while ( token != NULL )
{
    if (strlen(token) > 0)
    {
        cout << token << endl; // to test if the token is correct so far.
        data[Tcount++] = *token;
    }
    token = strtok(NULL, " ");
}


for(i = 0; i < Tcount; i++)
{
    cout << data[i] << endl;
}  

For some reason when i enter in a user input of xp = a + 1, the output of data[i] is:
x
=
a
+
1
Do you know why the first token (should be xp) is only being stored in data[] as x?
Thanks.

3 Answers3

1

strtok has some internal state that you aren't taking into account. See here:

http://www.cplusplus.com/reference/cstring/strtok/

This line only grabs the first character of the token:

    data[Tcount++] = *token;

Then this line skips to the next token (due to internal state of strtok remembering location of last token):

    token = strtok(NULL, " ");

You need to nest another loop inside the while loop that contains these two lines, so that you read all the characters in a token. Only then can you call strtok again without losing data.

Barry Gackle
  • 829
  • 4
  • 17
  • I updated the code to include a nested loop. I also included a check to see if the tokens are being created properly and they are. The section ' cout << token << endl; ' is printing tokens of more than 1 character e.g. xp. However, the array data[i] still is only printing out the first character (x). I think its due to what @farukdgn has said that i need to create a string array, although im still having problems doing this. – user4878925 May 11 '15 at 00:46
0

Because your token array is a char array (*char). You need to create a string array (**char) to store "xp" since it is not a char variable.

omerfarukdogan
  • 839
  • 9
  • 26
0

There are a few things not quite correct.

  1. The part "data[Tcount++] = *token;" is assigning a value of "token" (char) to a "data[Tcount]" which is also a char.

  2. I believe that the "data" contains an address to the start of 100 * sizeof (char) in the memory. And when "data[i]" is accessed it is a single "char"

for(i = 0; i < Tcount; i++) { cout << data[i] << endl; }

  1. If you want to store a list of strings (an array of chars), you need to use 2 dimensional arrays (or pointers) so each array (or pointer) can store an array (or pointer). For example, use data[][] or **data. If you would like to refresh definitions of these please refer to the links below;

click reference to pointer of pointer in c++.

click reference to arrays in c++.

workaholic
  • 31
  • 3