How do I stop this recursion from looping too much?

So this is for an assignment, thus I can only post a small part of my code. I don't need help for the assignment itself, just wanna fix this part of it.

The general idea is that I have two variables (x and y) which get incremented/decremented when a letter in a string has been reached. Then there are two more variables with different values (finalx and finaly). The first variables need to have the same values as the last ones for the base case to be reached. If the first two variables are the same as the others two we return true for a bool variable.

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
#include <iostream>
#include <string>
#include <stdexcept>

using namespace std;

void count(int iter, int x, int y, int finalx, int finaly, string letters, bool& result)
{
        if ((x == finalx) && (y == finaly))
        {
                result = true;
                return;
        }
        else
        {
                for (int i = iter; i < letters.size(); i++)
                {       
                        if (letters.at(i) == 'l')
                        {       //cout << y;
                                count((iter + 1), (x), (y - 1), finalx, finaly, letters, result);
                        }
                        else if (letters.at(i) == 'r')
                        {       //cout << y;
                                count((iter + 1), (x), (y + 1), finalx, finaly, letters, result);
                        }
                        else if (letters.at(i) == 'u')
                        {       //cout << x;
                                count((iter + 1), (x - 1), (y), finalx, finaly, letters, result);
                        }
                        else if (letters.at(i) == 'd')
                        {       //cout << x;
                                count((iter + 1), (x + 1), (y), finalx, finaly, letters, result);
                        }
                }
        }
}

int main() {

    int iter = 0, x = 9, y = 2, finalx = 0, finaly = 3;
    bool result;
    string letters = "uulluurrruuuulul";

    count(iter, x, y, finalx, finaly, letters, result);

    cout << result;

    return 0;
}

You can see the X and Y in the main function. the string will get checked for every letter in it and if let's say we reach u then X will get decremented and the function will start again with those changed variables.
The problem is that even though it reaches the proper result when I use smaller numbers for the 4 variables (and a shorter string) it loops way too much until it does that, and it never seems to stop for the variables posted in the example above.
Where is the mistake? It's probably something really stupid (maybe the variables need to be referenced?) but I can't figure it out. Help me please.
Last edited on
It is the for(...) loop. Since you already advance the index (iter + 1) you do this multiple times within the loop where iter does not change. So remove the loop.
Lol yup, you're right. I rewrote the function in a much simpler way and it works now. Thanks!
Topic archived. No new replies allowed.