1

I am having trouble building a tokenizer. I am new to c++ and was wondering if anyone could help.

When I run the program, I enter the user input as "x = a + 1". When i do this, the only token output is the x. I want to display "x\n = a\n +\n 1\n"

#include <iostream>
#include <string>
using namespace std;

int main(void)
{

 char *text = (char*)malloc ( 40 *sizeof( char ));
 cout << "Enter the first arrangement of data." << endl;
 cin >> text;
 char *token = strtok(text, " ");
 while ( token )
 {
     if ( strlen(token) > 0 )
     {
        printf(" %s", token);
     }
     token = strtok(NULL, " ");
 }
 return 0;
}
kdopen
  • 8,032
  • 7
  • 44
  • 52

1 Answers1

0

You are not reading the entire input.

Change

cin >> text;

to

cin.getline(text, 40);
olovb
  • 2,164
  • 1
  • 17
  • 20