Nested While Loops with Pointers

If I have two while loops nested, the inner one should have the precedence compared to the outer one, is that correct?

Because when I am trying to run this program that should match every char of a word in the free store to another one always in the heap, it runs only for first letter "B" then it returns me 0. I have tried to modify the two loops but without success. Can you help me please?

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
char* findx(const char* s, const char* x)
{
	int count_s = 0;
	int count_x = 0;

	while (s[count_s] != 0)
	{
		while (x[count_x] != 0)
		{
			if (s[count_s] == x[count_x])
			{
				cout << "Found match " << x[count_x] << " at S[" << count_x
					<< "] with " << s[count_s] << " at X[" << count_s 
					<< "]" << endl;
			}
			++count_x;
		}
		++count_s;
	}

	cout << "Match " << *x << " not found" << endl;
	return 0;
}

int main()
{
	char* p = new char[5]{ "Casa" };
	char* p1 = new char[9]{ "Broccoli" };
	findx(p, p1);
	delete[] p;
	delete[] p1;
	keep_window_open();
	return 0;
}


It actually runs only for the first letter "B" returning me, correctly, "Match B not found" but does not continue iterating!

Thank you
Last edited on
The problem is that count_x is not set to 0 before the inner loop starts (the second time). Better use a for loop.
Thanks for reply coder777. It is part of an exercise where I have to build a function that works without the help of any other existing function of the standard library or any other kind of help. How could I fix it?
In your code your output statement always executes once at the end of the function and always writes first letter of character array x.

It is actually iterates fully, but there is another bug here:
what is the value of count_x when inner loop finishes (after line 17)? What happens next? Try to reason what happens step-by-step noting values of each variable after each line.

EDIT: I was too late :)
Last edited on
This solves the issue that the inner loop doesn't run the second time:
1
2
3
4
5
6
7
8
9
10
11
char* findx(const char* s, const char* x)
{
	int count_s = 0;
	int count_x = 0; // Note

	while (s[count_s] != 0)
	{
		int count_x = 0; // Note
		while (x[count_x] != 0)
...
}
Thanks, I solved! I also missed a break .

That's updated code:

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
char* findx(const char* s, const char* x)
{
	int count_s = 0;

	while (s[count_s] != 0)
	{
		int count_x = 0;
		while (x[count_x] != 0)
		{
			if (s[count_s] == x[count_x])
			{
				cout << "Found match " << x[count_x] << " at X[" << count_x
					<< "] with " << s[count_s] << " at S[" << count_s
					<< "]" << endl;
				return 0;
				break;
			}
			++count_x;
		}
		++count_s;
	}

	cout << "Match " << *x << " not found" << endl;
	return 0;
}

int main()
{
	char* p = new char[5]{ "casa" };
	char* p1 = new char[9]{ "Broccoli" };
	findx(p, p1);
	delete[] p;
	delete[] p1;
	keep_window_open();
	return 0;
}
Last edited on
You don't actually need the break because return ends the function.
Topic archived. No new replies allowed.