Hi,
I created an "address" structure. Each address (xx.yy.zz.mm) consists of an xx, yy, zz and mm element, all of which are ints. Each address also has a "name" element associated with it.
I have an array of up to 100 addresses called "network". Here is an example of some elements in network:
186.88.1.21 Tyler
186.88.9.11 Bob
101.21.0.13 Tom
111.11.3.89 Chuck
101.21.5.99 Luke
I need to check each address and see if there are other addresses from the same location. Two addresses are from the same location if elements xx and yy are identical. If there are 1 or more addresses from the same location, I need to output this information.
Below is some code I wrote to try to do this:
char temp[11];
int nameCount;
for (i = 0; i < count; i++)
{
char names[100][10] = {};
strcpy(temp, network[i].name);
temp[11] = '\0';
nameCount = 0;
for (j = i + 1; j < count; j++)
{
if (network[i].xx == network[j].xx && network[i].yy == network[j].yy)
{
strcpy(names[nameCount], network[j].name);
nameCount++;
}
}
if (nameCount == 0)
printf("No matches for %s.\n", temp);
else
{
printf("%s ", temp);
for (j = 0; j < nameCount; j++)
printf("and %s ", names[i]);
printf("are from the same location.\n\n");
}
}
This code works for the first two addresses in the array which are from the same location, but it doesn't work for the rest (although it looks like it almost does -- it's printing blanks instead of names, but it has the right number of blanks). The output for the addresses I listed above is (sorry for the bad formatting):
Tyler and Bob are from the same location. No matches for Bob . Tom and [space] and [space] are from the same location. No matches for Chuck . Luke and [space] are from the same location. No matches for Nick .
It also seems like there is a newline character that has been added to the end of each name.
Any ideas?