New to c++ need minor help with code

Can some one point out what I should do to if two products have the same price then the former one is more expensive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  #include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;



void getProduct(vector<string> &names, vector<int> &numbers, vector<double> &prices);
void displayProduct(vector<string> &names, vector<int> &numbers, vector<double> &prices, int &index);
void mostExpensive(vector<string> &names, vector<int> &numbers, vector<double> &prices);

int main() {

	vector<string> productNames;
	vector<int> productNumbers;
	vector<double> productCosts;

    cout << "Reading inventory:" << endl;
	for (int i = 0; i < 5; ++i) {
		getProduct(productNames, productNumbers, productCosts );
	}

	cout << "Displaying inventory:" << endl;
	for (int i = 0; i < 5; ++i) {
		displayProduct(productNames, productNumbers, productCosts, i);
	}

	mostExpensive(productNames, productNumbers, productCosts);


	return 0;
}


void getProduct(vector<string> &names, vector<int> &numbers, vector<double> &prices) {

	cout << "Name: "  ;
	string productName;
	cin >> productName;
	names.push_back(productName);

	cout << "Number: " ;
	int productNumber;
	cin >> productNumber;
	numbers.push_back(productNumber);

	cout << "Price: ";
	double productPrice;
	cin >> productPrice;
	prices.push_back(productPrice);
	cout << endl;

}

void displayProduct(vector<string> &names, vector<int> &numbers, vector<double> &prices, int &index) {
	cout << "Name: " << names[index] << endl;
	cout << "Number: " << numbers[index] << endl;
	cout << "Price: " << prices[index] << endl;
	cout << endl;
}

void mostExpensive(vector<string> &names, vector<int> &numbers, vector<double> &prices) {
	cout << "Most expensive product: " << endl;
	int indexOfMax = 0;
	int currMax = 0;

	for (int i = 0; i < 5; ++i) {
		if (prices[i] > currMax) {
			currMax = prices[i];
			indexOfMax = i;
		}
	}

	displayProduct(names, numbers, prices, indexOfMax);

}
The code already selects the former as the max if items are the same price.

I would suggest that you simplify this code though, if you would like I could show you how I would do it.
that would be great if you could show me how you would do it but still my program seems to be piking the later expensive donĀ“t know why...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

class product {
private:
  string name;
  int number;
  double price;

public:
  friend bool operator<(const product& x, const product& y) { return x.price < y.price; }
  friend bool operator>(const product& x, const product& y) { return y < x; }
  friend bool operator<=(const product& x, const product& y) { return !(y < x); }
  friend bool operator>=(const product& x, const product& y) { return !(x < y); }

  friend ostream& operator<<(ostream& os, const product& x) {
    os << "Name: " << x.name << "\n";
    os << "Number: " << x.number << "\n";
    os << "Price: " << x.price << "\n";
    return os;
  }

  friend istream& operator>>(istream& is, product& x) {
    cout << "Name: ";
    is >> x.name;
    cout << "Number: ";
    is >> x.number;
    cout << "Price: ";
    is >> x.price;
    return is;
  }
};

int main() {
  vector<product> products;
  cout << "Reading Inventory:\n";
  for (int i = 0; i < 5; ++i) {
    product tmp;
    cin >> tmp;
    products.push_back(tmp);
    cout << endl;
  }

  cout << "Displaying Inventory:\n";
  for (const auto& p : products) {
    cout << p << endl;
  }

  cout << "Most Expensive Product:\n";
  cout << *max_element(begin(products), end(products)) << endl;
}


This is how I would handle, not that I don't do any error handling, purely an example.
I use max_element which will return an
Iterator to the greatest element in the range [first, last). If several elements in the range are equivalent to the greatest element, returns the iterator to the first such element. Returns last if the range is empty.

I actually think that this is wrong, that it should return the last such element, but it will work for what you need.
Topic archived. No new replies allowed.