How to stop program sequence if it reaches its target

Hello. I need help with this exercise, been trying to figure it out for the last 2 hours and this last bit specifically is what I keep failing at.

Gist of the exercise:
/////////////////////
A rover which is at specific coordinates (x0, y0) is being sent movement commands. It has to reach the point (x1, y1). Commands are coded with number 1-4 (1 is up, 2 is right, 3 is down and 4 is left)(with each command it moves by one square).
Write a program that would tell after checking the movement commands if the rover reached its destination and the commands it completed.
\\\\\\\\\\\\\\\\\\\\\

This is the example of data file:
5 -1 // Starting coordinates
8 -3 // Destination coordinates
3 // Amount of command sequences
7 2 3 2 3 1 3 2 // Command sequences (first number in the line is the length of the actually sequence.
2 1 4
12 2 3 2 3 2 3 2 3 2 3 2 1

And this is how the result should look like:
destination reached 2 3 2 3 1 3 2 7
sequence ended 1 4 2
destination reached 2 3 2 3 2 5


What I managed to do earlier was to get results, but it would go through whole sequence, when I need it to stop if it reaches its destination
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 #include <iostream>
#include <fstream>

using namespace std;

struct marsaeigis
{
    int seka[30]; // storing command sequence in array for easy access
};

int main()
{
    ifstream duom ("U2.txt");
    marsaeigis kelione[10]; // Because of multiple rovers, we need multiple instance of the commands

    int x0, y0; // starting coordinates
    duom >> x0;
    duom >> y0;

    int x1, y1; // destination coordinates
    duom >> x1;
    duom >> y1;

    int n; // lenght of the commands sequence 
    duom >> n;
    int k[n]; // sekos ilgis
    for (int i=0; i<n; i++)
    {
        duom >> k[i];
        for (int j=0; j<k[i]; j++)
        {
            duom >> kelione[i].seka[j];
        }
    }
    int xGal[n]; // array for the destination, because each rover starts at (0;0)
    xGal[n]=x0;
    int yGal[n];
    yGal[n]=y0;
    int s[n]; // counter for completed commands
    
    // Can't get it to work from here
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<k[i]; j++)
        {
            if ((xGal[i]!=x1)&&(yGal[i]!=y1))
            {
                if (kelione[i].seka[j]==1)
                {
                    yGal[i]=yGal[i]+1;
                    s[i]++;
                }
                else if(kelione[i].seka[j]==2)
                {
                    xGal[i]=xGal[i]+1;
                    s[i]++;
                }
                else if (kelione[i].seka[j]==3)
                {
                    yGal[i]=yGal[i]-1;
                    s[i]++;
                }
                else if (kelione[i].seka[j]==4)
                {
                    xGal[i]=xGal[i]-1;
                    s[i]++;
                }
            }
        }
    }

    for (int i=0; i<n; i++)
    {
        for (int j=0; j<s[i]; j++)
        {
            if ((xGal[i]==x1)&&(yGal[i]==y1))
            {
                cout << "Tikslas pasiektas ";
                cout << k[j];
            }
            else if((xGal[i]!=x1)&&(yGal[i]!=y1))
            {
                cout << "Sekos pabaiga" << endl;
            }
        }
    }

    return 0;
}
one way you can stop by adding more conditions to your for loops.
eg
for( what you have already && currentpositon != destination)
for(what you have already && currentpositon != destination)
{ many if statments etc UNCLEAR: DO YOU NEED TO CHANGE ALL THE IF STATEMENTS TO ALSO CHECK IF AT DESTINATION?
}

I do not see exactly WHAT that condition would be in your code, but you should know what to do there.

Another way is a goto that jumps out of the loops when you are done. This is less code, and may be justifiable here.
Last edited on
Topic archived. No new replies allowed.