Deleting number from array

Hello, I have this txt file: book name, number of books price of book
Lorf Of The Rings, 5 10.00
Dracula, 6 15.99
Emma, 7 12.58
Romeo And Juliet, 9 50.00
Lorf Of The Rings, 6 8.50
Romeo And Juliet, 2 29.99

And I neet to make a file list in which need to be deleted same books.
So my results need to be:
Lorf Of The Rings, 5 10.00
Dracula, 6 15.99
Emma, 7 12.58
Romeo And Juliet, 9 50.00

my code
1
2
3
4
5
6
7
for(int i=0; i<n; i++){
    for(int j=i+1; j<n; j++){
	if(K[i].BookName() == K[j].BookName()){
	   K[i].BookName() = K[j].BookName();
        }
    }
}

Last edited on
Where's the problem? Please be specific.
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
Last edited on
But it only does so if K[i] == K[j], so in the end no change is made.
Oh... Right!, but now I'm distressed. Is it possible to do what I need to?
Last edited on
But wait,
1
2
3
4
5
6
7
8
9
for(int i=0; i<n; i++){
    for(int j=i+1; j<n; j++){
	if(K[i].BookName() == K[j].BookName()){
	   K[i].BookName() = K[j].BookName();
cout << K[i].BookName() << endl;
        }
    }

}

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?


Last edited on
Any ideas? It would be realy nice if something give a good idea how to do that? :/
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
        }
    }
}
Last edited on
Ok thanks, here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	for(int i=0; i<n; i++){
		for(int j=i+1; j<n; j++){
			if(K[i].BookName() == K[j].BookName()){
				string bookname = K[i].BookName();
				K[i].SetBookName(bookname);
				K[j] = K[n-1];
				n--;
				j--;
			if(K[i].Price() <= K[j].Price()){
				double price = K[j].Price();
				K[i].SetPrice(price);
				K[j] = K[n-1];
				n--;
				j--;
		    }
			}
		}
		cout << K[i].BookName() << " " << K[i].Price() << endl;
	}


And why my program should cout book name and bigest price of book. Andhere is my txt files
Trys nykštukai, 5 10.00
Mikė pūkuotukas, 6 15.99
Grybų karas, 10 12.10
Trys nykštukai, 4 8.50
Grybų karas, 3 19.99

And I get results
Trys nykštukai 12.1
Mikė pūkuotukas 15.99
Grybų karas 19.99

And one price of Trys nukštukai is wrong.... should be 10.00 not 12.1... what's wrong?
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.
Topic archived. No new replies allowed.