I am trying to compare two arrays to find duplicate values in them and be able to display them. I can't seem to be able to display the duplicate values between them. Here is my code any help is appreciated.
while (x < c && y < d)
{
if (chiwarehouse[c] == detwarehouse[d]) //using wrong variables for the check
{
cout << "\n" << detwarehouse[y];
x++;
y++;
}
}
It should be: if (chiwarehouse[x] == detwarehouse[y])
PS - Will this code work if the user enter different valuesfor the Chicago and Detroit numbers????
It should work with other numbers but it's not working at the moment. When I did what you told me to do the program would just sit there and I would have to close it out manually. After asking for some advice someone told me to try this instead but this doesn't work properly as it only displays the first number in the chiwarehouse array when is sorted and not the duplicate values between them.
Ah, Its Sunday, my brain must have been having a day off.
I should have noticed that your x++ and y++
was in the wrong place, and was only being increment if there was a duplicate match - that was why the program was hanging
So for the record - the original should have been:
1 2 3 4 5 6 7 8 9 10 11
while (x < c && y < d)
{
if (chiwarehouse[x] == detwarehouse[y])
{
cout << "\n" << detwarehouse[y];
}
// x and y should be updated here.
x++;
y++;
}