Dissecting a string for data

Hi guys.

I have a project for my C++ class. It requires the building of a very basic drawing program that takes inputs to the program from a txt file and creates them with an OpenGL function. The drawing side is not the problem, I am having some issues with the input and dealing with it. Consider the following input file:

FORWARD 10
RIGHT 90
FORWARD 20

this tells the program, draw a 20 unit length line, rotate 90deg then draw another line. Great! What I am doing is reading in to a string, then using some stringstream functions to treat that string as an input source,and checking for the space bar delimeter to give me two strings which I will then use to initiate a 'Forward' object with a unit value as given in the second string. This code sums it up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    list <string> thelist;

    string str = "FORWARD 10";
    string word;
    
    stringstream stream(str);
    
    while( getline(stream, word, ' ') ) 	
        thelist.push_back(word);

    list<string>::iterator i;
    
    for(i=thelist.begin(); i != thelist.end(); ++i) 
        cout << *i << "\n";


That works fine and I end up with a list of strings with FORWARD and 10 in it, great.

The problem is that the program also requires the use of a REPEAT keyword that can be chained together e.g.

REPEAT 10 [ FORWARD 10 RIGHT 90 ]
or more fiendishly:
REPEAT 10 [ FORWARD 10 REPEAT 10 [ FORWARD 5 RIGHT 90 ] ]

Now obviously I am going to need some recursion here but I am asking for peoples general opinions about how best to split up a long string as above, allowing for the spaces and brackets to be ignored and for recursion.

Considering the first REPEAT input, I'm thinking that if there is some way I can create a new string which contains everything after the [ bracket, then I can send a reference to this string to a repeating function and go from there. However I don't know how I would go about re-creating a string that consists of just some of a list of strings, bearing in mind that I took the original string and broke it up and put them in a list.... I hope that makes sense guys. If youre interested my tutors web page is here for the assignment: http://personal.ee.surrey.ac.uk/Personal/R.Bowden/cpp/assignment/assignment.html
Topic archived. No new replies allowed.