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);
}
|