Gawkzz - 1 year ago 68

C++ Question

I am having some issues with getting my array to iterate through in my for loop.

The point of this function is to check if the number has already been placed into the array somewhere else so no duplicates can appear.

when I run through the function it only iterates through the first element of the function and stops.

`bool check(int wins[], int number)`

{

for (int i = 0; i <= arraySize; ++i)

if (number == wins[i])

{

return true;

}

else if (number != wins[i])

{

return false;

}

}

I really appreciate the help.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Your indentation is misleading. Here what is happening:

```
bool check(int wins[], int number)
{
for (int i = 0; i <= arraySize; ++i)
if (number == wins[i])
{
return true;
}
else if (number != wins[i])
{
return false;
}
}
```

So in first iteration, you, either number is equal to the element or it's different, but in both case you return and the function is finished !

If you want to search your item, you have to do a slight change:

```
bool check(int wins[], int number)
{
for (int i = 0; i < arraySize; ++i) // assuming that arraySize is some const size of the array
if (number == wins[i]) // if number is found
{
return true; // return immediately
}
// if you arrive here, the loop finished without a match
return false;
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**