15,881,248 members
See more:
Hello all.

I have two list boxes. One of them is filled with file names and their directories.

I want to count all duplicate file names in the first list box and put the file name and the number of duplication in the second list box.

like: 0.TXT (6)

the program:http://img178.imageshack.us/img178/2160/82415716.png[^]

I use VC++, so please give me C++ code for I'm still a beginner. :((

Posted

## Solution 1

check that link even it is in vb.net but you can understand the algorithem of it :

the trick is to create 2 nested loops :
while(i<listbox.count){>
for(k<listbos.count){>
// then check if(listbox[i]==listbox[k]){
then add this item to the new list ...
}}
regards...

v2

## Solution 2

thanks a lot for the answer but I really couldn't understand many of it's expressions.

I googled it but all I found was VB code.

and about the idea you provided above, I tried it but it gives me an out of range exception.

here is what I did so far:

```int L = listBox1->Items->Count::get();
int i = 0;
while(i<l)>
{
i++;
for(int k=0; k<l;>
{
if(listBox1->Items[i]==listBox1->Items[k])
{
}
}
}```

Sorry if I were wrong!

v4
DaveAuld 7-Nov-10 7:19am

## Solution 3

yes sure it will give out of range I just gave an idea without giving the fulllogic ...
the trick is to start in the second loop where we have reached the last time ...

here is the logic :

for ( i=0;i<count;i++){>

for( j=i+1;j<count-i;j++){>

then do what ever you want }
}

regards...

DaveAuld 7-Nov-10 7:21am
aim999 7-Nov-10 8:19am
Well, it didn't work either. Am I missing something?
for(int i=0; i<l; i++)
for(int="" j="i+1;" j<(l-i);="" j++)
Tamer Hatoum 7-Nov-10 8:34am
what is the error ??? can you post all of your code???
aim999 7-Nov-10 10:23am
Yes, sure but the comment block is not enough and when I post it as an answer it gets altered

## Solution 4

C++
```private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
for(int x=0; x<=10; x++)
{
}
for(int y=0; y<=5; y++)
{
}
for(int z=0; z<=3; z++)
{
}
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
int L = listBox1->Items->Count::get();
for(int i=0; i<l;>                 {
for(int j=i+1; j<=(L-i); j++)
{
if(listBox1->Items[i]==listBox1->Items[j])
{
}
}
}```

It doesn't give an error and doesn't do anything too.

v3
Tamer Hatoum 7-Nov-10 11:53am
acctually it looks fine ... but can you add some breaks and debug your error for inserting some alerts in each loop to know what is happening and inform me back.. thanks...
For (i = 0 ;List.Items.Count - 2 ) {
For (j = ListItems.Count - 1 ; i + 1) {
If( List.Items(i).ToString = List.Items(j).ToString){you code here}}}
and also try that link please I think it will help u a lot
regards...
aim999 8-Nov-10 4:13am
- the compiler does not understand (List) in:
List <string^>^listDuplication = gcnew List();
- and (one) in:
if (one == strFile2)
-I think the other errors will be fixed if those are. I don't know why the code alters when I paste it here.
thanks again