Consider the following list:
Name Price(per kg) Weight(in kg)
rice1 40 20
rice2 50 27
rice3 35 24
I want all the rice types sorted according to their value. So, I sort them.
new sorted price: 35 40 50
Now i print them and the output as below:
Name Price Weight
rice1 35 20
rice2 40 27
rice3 50 24
But this is not what I wanted. I wanted it to print like this:
Name Price Weight
rice3 35 24
rice1 40 20
rice2 50 27
so, the problem is I'm getting the the value in the sorted list but not the names and weights. I want to get sorted everything according to value. How can i do that?
I've written the following code. But not sure what to do next.
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
class treasure
{
public:
std::string name[100];
double value[100];
double weight[100];
};
int itemNumber, totalWeight, i;
treasure item;
std::cout << "Enter total item weight(in kg): " << std::endl;
std::cin >> totalWeight;
std::cout << std::endl <<"Enter total item number: " << std::endl;
std::cin >> itemNumber;
//take item name, item value, item weight
for( i = 0; i < itemNumber; i++)
{
std::cout << std::endl << "Enter item name: " << "\t" << "Enter item value(per kg): " << "\t" << "Enter item weight(in kg): " << std::endl;
std::cin >> item.name[i] >> item.value[i] >> item.weight[i];
}
//sort items according to given value
std::sort(item.value, item.value + itemNumber);
//print sorted list
for( i = 0; i < itemNumber; i++)
{
std::cout << std::endl << std::endl << "Item name: " << "\t" << "Item value(per kg): " << "\t" << "Item weight(in kg): " << std::endl;
std::cout << item.name[i] << "\t\t" << item.value[i] << "\t\t\t" << item.weight[i] << std::endl;
}
return 0;
}