Ok I have a program that reads in a .TXT file and displays it as tokens.

What I'm trying to do now is read the file and print out only the reserved words for C++ and the variable it uses.

For example:

int main()
{

double e = 24;
int i = 12;

}

would print out like this

double e
int i


What I have in mind is to create a string that holds the keywords I need to work with and then when I store the token have it check for the keyword, if it is there then store that & it's variable in a 2-d array. Then ignore everything else.

My issue is I have my current token storage declared as a string and when I attempt to put that value into a string array it won't let me.

So my question is what would be the simplest way to accomplish the above. Or basically how would I go about copying the string to the array??

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

I would first write down all the possible reserved words in c++.

Then I would search for lines that contain them. Then parse them.

Normally there would be a space separating the reserved word and its actual variable.

That could be a clue how to go about this... But I'm not sure on its reliability for example, int main would be one such exception.

In regards to copying the string into a string array or vector it should be trivial. With a vector you would just use the push_back() method.

I think it is very unlikely that the best solution to this problem will involve a two-dimensional array. An array of structures, perhaps, or (better) a vector of structures; but not a two-dimensional array.

How about posting a code fragment that illustrates your problem?

1)Find all C++ reserved keywords online.
2)Insert the reserved keywords in order into map.
3)Read in word by word from the text file
3.a) For each word check if it needed word exist in the map
3.a.1) If not then "forget about it"
3.a.2) If so then doStuff with it

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.