I am writing a code that utilizes a nested loop, just one for loop inside another. Within the inner loop, I need an if statement that if true, would stop the inner loop but not the outer. I have tried many things such as break, continue, and even goto, but nothing seems to work. Here is the nested loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
for (int i = 0; i < strlen(text); i++)
{
int counter = 0;
for (int y = 0; y < strlen(text); y++)
{
if (text[i] == text[y])
counter++;
}
for (int y = 0; y <= i; y++)
{
if (i == 0)
cout << counter << " " << text[i] << endl;
if (text[i] == text[y])
// Inner for-loop needs to stop here if true.
if (y == i)
cout << counter << " " << text[i] << endl;
}
}
I have located the problem. For some reason, (text[i] == text[y]) is always true, but only in the for statement at 9 and not the one at 4. Any ideas as to what is wrong in that for loop?