    001: #include <iostream>
    002: #include <string>
    003: #include <vector>
    004:
    005: using namespace std;
    006:
    007: #include <iostream>
    008: #include <string>
    009:
    010: using namespace std;
    011:
    012: class Product
    013: {
    014: public:
    015:    /**
    016:       Constructs a product with zero price and score.
    017:    */
    018:    Product();
    019:
    020:    /**
    021:       Reads in this product object.
    022:    */
    023:    void read();
    024:
    025:    /**
    026:       Compares two product objects.
    027:       @param b the object to compare with this object
    028:       @retur true if this object is better than b
    029:    */
    030:    bool is_better_than(Product b) const;
    031:
    032:    /**
    033:       Print this product object
    034:    */
    035:    void print() const;
    036: private:
    037:    string name;
    038:    double price;
    039:    int score;
    040: };
    041:
    042: Product::Product()
    043: {
    044:    price = 0;
    045:    score = 0;
    046: }
    047:
    048: void Product::read()
    049: {
    050:    cout << "Please enter the model name: ";
    051:    getline(cin, name);
    052:    cout << "Please enter the price: ";
    053:    cin >> price;
    054:    cout << "Please enter the score: ";
    055:    cin >> score;
    056:    string remainder; /* read remainder of line */
    057:    getline(cin, remainder);
    058: }
    059:
    060: bool Product::is_better_than(Product b) const
    061: {
    062:    if (price == 0) return false;
    063:    if (b.price == 0) return true;
    064:    return score / price > b.score / b.price;
    065: }
    066:
    067: void Product::print() const
    068: {
    069:    cout << name
    070:       << " Price: " << price
    071:       << " Score: " << score << "\n";
    072: }
    073:
    074: int main()
    075: {
    076:    vector<Product> products;
    077:
    078:    Product best_product;
    079:    int best_index = -1;
    080:
    081:    bool more = true;
    082:    while (more)
    083:    {
    084:       Product next_product;
    085:       next_product.read();
    086:       products.push_back(next_product);
    087:
    088:       if (next_product.is_better_than(best_product))
    089:       {
    090:          best_index = products.size() - 1;
    091:          best_product = next_product;
    092:       }
    093:
    094:       cout << "More data? (y/n) ";
    095:       string answer;
    096:       getline(cin, answer);
    097:       if (answer != "y") more = false;
    098:    }
    099:
    100:    for (int i = 0; i < products.size(); i++)
    101:    {
    102:       if (i == best_index) cout << "best value => ";
    103:       products[i].print();
    104:    }
    105:
    106:    return 0;
    107: }