Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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. :((

Thanks in advance. :-O
Posted

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

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...
 
Share this answer
 
v2
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])
    {  
      listBox2->Items->Add(listBox1->Items[i]);
    }
  }
}


Sorry if I were wrong!
 
Share this answer
 
v4
Comments
DaveAuld 7-Nov-10 7:19am    
This is not an answer, use the Add Comment feature below the answer you want to comment against...........
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...
 
Share this answer
 
Comments
DaveAuld 7-Nov-10 7:21am    
Do not use the Add Answer to have a discussion, use the Add Comment feature below the question or answer that you are discussing.....
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++)
if(listbox1-="">Items[i]==listBox1->Items[j]) listBox2->Items->Add(listBox1->Items[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
C++
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 for(int x=0; x<=10; x++)
                 {
                     listBox1->Items->Add("D:\\QuakeII\\"+x+".TXT");
                 }
                 for(int y=0; y<=5; y++)
                 {
                     listBox1->Items->Add("E:\\Programs"+y+".TXT");
                 }
                 for(int z=0; z<=3; z++)
                 {
                     listBox1->Items->Add("F:\\Videos\\"+z+".TXT");
                 }
             }
    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])
                         {
                             listBox2->Items->Add(listBox1->Items[j]);
                         }
                     }
                 }


It doesn't give an error and doesn't do anything too.
 
Share this answer
 
v3
Comments
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}}}
try that please...
and also try that link please I think it will help u a lot
http://social.msdn.microsoft.com/Forums/en/winforms/thread/07ba9f58-dd22-4c73-b4ff-eb9d38362e2a
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900