2

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;
}
Insane
  • 33
  • 3

1 Answers1

2

Your structure should look like this:

class treasure
{
    public:
        std::string name;
        double value;
        double weight;
};

and then you declare array of treasures:

treasure item[100];

taking items:

//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[i].name >> item[i].value >> item[i].weight;
}

and finally sorting:

std::sort(item, item + itemNumber,
    [] (auto t1, auto t2) {return t1.value < t2.value;})

http://ideone.com/GL3VKz

Zefick
  • 2,014
  • 15
  • 19
  • Showing this errors and 33 line is the line starting with [ ] `||In function 'int main()':| |33|error: expected primary-expression before '[' token| |33|error: expected primary-expression before ']' token| |33|warning: C++0x auto only available with -std=c++0x or -std=gnu++0x| |33|error: expected primary-expression before 'auto'| |33|warning: C++0x auto only available with -std=c++0x or -std=gnu++0x| |33|error: expected primary-expression before 'auto'| |36|error: expected ';' before 'for'| |36|error: expected ';' before ')' token| ||=== Build finished: 6 errors, 2 warnings ===|` – Insane Feb 16 '17 at 16:53
  • Error text says directly that code shoul be compiled using C++11 standard. It enabled with -std=C++0x compilation flag in g++. Modern compilers use C++11 or C++14 by default. Here is the same axample written in C++98: http://ideone.com/WV25W7 – Zefick Feb 16 '17 at 17:33