This code you presented does nothing. The operation in line 4 assigns to K[i] the same value it already has. You probably want to remove either K[i] or K[j] when they are the same.
I need to remove the same book on txt file. And I don't understand why my code does nothing?
The operation in line 4 assigns to K[ i ] = K[ j ], where i =0 ank j = 1 ant the beginning
And I get results
Lord Of The Rings
Dracula
So my program work, but when i I try to cout BookName at 8 line I get the same results like in my txt file. I understand why, but how I need to make a orderly list?
deividas717:
And I get results
Lord Of The Rings
Dracula
So my program work,
If it is so, it does not work - Dracula is not duplicate, while Romeo and Juliet is.
You must get rid of duplicates. Your code finds them, but how to remove them depends on what data type K really is. List, arrays, vectors all behave slighly different.
1 2 3 4 5 6 7 8 9 10 11 12
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(K[i].BookName() == K[j].BookName()){
cout << K[i].BookName() << endl;
// get rid of K[j] here. Eg. K[j] = null
// or shift elements of array left, to overwirte duplicate at K[j]
// or if K is vector use K.remove[K.begin() + j];
// be carefull - n should be reduced and j incremented
// after such operation
}
}
}
This is the first time that you mention price. Your elements have name, count, and price. How should the count be handled?
Some days ago there was (more than one thread) on a problem of removing repeating characters from a string. It had the additional restriction that the array was a C-array rather than a standard container. That algorithm would suffice here, with one extra twist: whenever a duplicate is found, update the price and count of the first occurrence if necessary before erasing the duplicate.