Storing the journey of a robot through two dimensional grid

Hi all,

I'm solving a project where I'm supposed to handle and record movements of a robot in a two dimensional grid, based on string input from the user. (only three characters control the robot movements - f = forward, h = turn right and v = turn left)

I've managed to take in strings, store them in a vector and access them and read in another for loop to move the robot. The thing is this solution is supposed to be able to take in more than one trip and that is where I am stuck in storing the movements of the robot.

I can't for the life of me think of a way to store each trip as it finishes where I can access it later before I take in the directions for the next one.

This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<int> x (10000);
    vector<int> y (10000);
    vector<string> trip;
    string temp;

    //How many trips will be made
    int howManyTrips(0);
    cin >> howManyTrips;

    //Push each trip into a vector of strings
    for (int i = 0 ; i < howManyTrips; i++)
    {
        cin >> temp;
        trip.push_back(temp);
    }

    //Copy each trip from the vector and read through it
    //to determine where the robot is going
    for (unsigned int i = 0 ; i < trip.size() ; i++)
    {
        string tempword = trip[i];
        int x(0);
        int y(0);

        //Compass => 1 = north, 2 = east, 3 = south, 4 = west
        int compass(1);

        for (unsigned int j = 0 ; j < tempword.length() ; j++)
        {
            if (tempword[j] == 'v' || tempword[j] == 'V'){
                compass--;

                if (compass < 1)
                    compass = 4;

            }
            if (tempword[j] == 'h' || tempword[j] == 'H'){
                compass++;
                if (compass > 4)
                    compass = 1;
            }
            if (tempword[j] == 'f' || tempword[j] == 'F'){
                switch (compass)
                {
                case 1:
                    y++;
                    break;
                case 2:
                    x++;
                    break;
                case 3:
                    y--;
                    break;
                case 4:
                    x--;
                    break;
                default:
                    cout << "Error!";
                    break;
                }
                cout << "X coordinate: " << x << " | Y coordinate: " << y << endl;
            }
        }
    }
}


The last cout was just to see if my implementation of controlling the robot was right, which it seems to be. If I try storing the coordinates after each F command in a vector it will be overwritten when trip two will be run...

Here is how this is suppose to work:

Input example:
2
HFFVFFH
HVVHVVFFHFFHFFHFFH

Output example:
2 2
0 0

Sorry for the long post, new on here and any help will be greatly appreciated!
will be overwritten when trip two will be run

Well, that is what you do on lines 30-34. Totally ignore the previous iterations of the loop.

If you had initialized the state only once, before the loop, then ...
Topic archived. No new replies allowed.