Hi,

I am kind of new to C++ and really stuck with a problem. Here I am trying to parse a string and after comparing to a pre-declared array save the tokens in a vector of vectors. It is giving a number of errors and I'm not sure what to change since I am using char* tokens in a another function as well. Could someone please tell me what is the best way to go about doing this and what changes i should make. Here is the function that is giving errors.

void parse(std::vector<char*>* tokens)
{
    std::vector<string> entity;

    int i=0, j=0, row=0;
    bool ent=false;

    //Initialisation of the program relationship model
    entity[0] = "stmt";
    entity[1] = "assign";
    entity[2] = "while";
    entity[3] = "if";
    entity[4] = "constant";
    entity[5] = "prog_line";
    entity[6] = "call";

    vector< vector<string> > matrix;

    /*for(int i=0; i<7; i++){
        matrix.push_back(vector <string>());
              matrix[i].push_back("xyz");
    }

    cout << matrix[2][0]; */ 

    //compare the tokens to the array entity
    for(i=0; i<tokens.size(); i++)
    {
        for(j=0; j<7; j++)
        {
            matrix.push_back(vector <string>());
            if(tokens[i] == entity[j])
                do{
                    i++;
                    if(tokens[i] != "," || tokens[i] != ";")
                    matrix[row][j] = tokens[i];
                }while(tokens[i] != ";");
        }        
    }            
}

You have declared tokens as a pointer to a vector:

std::vector<char*>* tokens

But are then trying to access the size() function as if it is an object (with the '.' operator):

tokens.size()

You need to instead use the -> operator:

tokens->size()

You also have to use

(*tokens)[i]

instead of

tokens[i]

David

commented: thnks a ton... :) +0

Your program is also inconcistent in the way it declares std namespace. If you don't use using namespact std; or using std::vector; then you have to preface every instance of vector with std::, such as std::vector< std::vector< std::string> > something; As for the parameter tokens -- unless that is an array of std::vector objects, then it would be better to dealred it as a reference instead of a pointer -- std::vector<std::string>& tokens) . That would remove the ambiguity between passing a reference to a single vector object or passing an array of vector objects. Declaring it as a pointer we don't know which one you meant without seeing the function that calld it.

Hey,
I have made the changes suggested and yet get a few errors, albeit fewer.
error C2664: 'strcmp' : cannot convert parameter 2 from 'char' to 'const char *'
I am not sure how to debug this.

My updated code is as follows:

void parse(std::vector<char*>* tokens)
{
    std::vector<char*> entity;

    int i=0, j=0, row=0;
    char* temp = new char[80];

    //Initialisation of the program relationship model
    entity[0] = "stmt";
    entity[1] = "assign";
    entity[2] = "while";
    entity[3] = "if";
    entity[4] = "constant";
    entity[5] = "prog_line";
    entity[6] = "call";

    std::vector<std::vector<char*>> matrix;
    /*for(int i=0; i<7; i++){
        matrix.push_back(vector <string>());
              matrix[i].push_back("xyz");
    }

    cout << matrix[2][0]; */ 

    //compare the tokens to the array entity
    for(i=0; i<tokens->size(); i++)
    {
        for(j=0; j<7; j++)
        {
            matrix.push_back(vector <char*>());
            if(strcmp((*tokens)[i], entity[j])==0)
                do{
                    i++;
                    if(strcmp((*tokens)[i], ',')==0 || strcmp(tokens[i],';')==0)
                    strcpy(temp, (*tokens)[i]);
                }while(strcmp((*tokens[i]), ';')==0);
        }        
    }            
}

Help is greatly appreciated. I am not really clear about the declaration of tokens. If needed I could post that function as well.

I don't know how to compare char arrays, I've never tried :)

std::vector<std::string>

Your program will get a runtime error because vector entity doesn't have any elements, so all those assignments in lines 9-15 will fail.

>>if(strcmp((*tokens), ',')

If you only want to compare two single characters to see if they are the same then why use strcmp()?? Just use == operator to compare them

for(i=0; i<tokens->size(); i++)
    {
        for(j=0; j<7; j++)
        {
            matrix.push_back(vector <char*>());
            if(strcmp((*tokens)[i], entity[j])==0)
                do{
                    i++;
                    if( *tokens->at(i) == ',' || *tokens->at(i) == ';')
                         strcpy(temp, tokens->at(i));
                }while(*tokens->at(i) == ';');
        }        
    }
commented: you're awesome!!! +0

Yes, I realised I should've used push_back.
I have one last question. How can i copy the temp value to matrix?

Would this work?

matrix[j].push_back(temp)

You've been a great help!! Thanks a ton. :)

I know C++ a little but I have been out of touch and I realised my concepts have become effy. Could you please recommend a good way to get a grasp over it? Any good books or sight?

Updated code:
On running this prints garbage values. :S

void parse(std::vector<char*>* tokens)
{
    vector<char*> entity;

    int i=0, j=0, row=0;
    char* temp = new char[80];

    //Initialisation of the program relationship model
    entity.push_back("stmt");
    entity.push_back("assign");
    entity.push_back("while");
    entity.push_back("if");
    entity.push_back("constant");
    entity.push_back("prog_line");
    entity.push_back("call");

    std::vector<std::vector<char*>> matrix;
    //compare the tokens to the array entity
    for(i=0; i<tokens->size(); i++)
    {
        for(j=0; j<7; j++)
        {
            matrix.push_back(vector <char*>());
            if(strcmp((*tokens)[i], entity[j])==0)
                do{
                    i++;
                    if( *tokens->at(i) == ',' || *tokens->at(i) == ';')
                    strcpy(temp, tokens->at(i));
                    matrix[j].push_back(temp);
                }while(*tokens->at(i) == ';');
        }        
    }

    for(i=0; i<7; i++)
        for(j=0; j<matrix[i].size(); j++)
            cout << matrix[i][j] << endl;

}

I have checked to print tokens. And it has correct values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.