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
|
#include <map>
#include <string>
#include <iostream>
struct Info{
std::string desc;
double price {};
};
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
std::cout << "Not an integer\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return i;
}
int main()
{
const std::map<int, Info> stock {{ 100, {"Remote control, timer, and stereo", 1000} },
{200, {"The same as Model 100, plus picture-in-picture", 1200}},
{300, {"The same as Model 200, plus HDTV, flat screen, and a 16 x 9 aspect ratio", 2400}}};
std::cout << "The available models are:\n";
for (const auto& [model, info] : stock)
std::cout << model << '\n';
std::cout << '\n';
const auto model {getInt("Enter model number: ")};
if (const auto fnd {stock.find(model)}; fnd != stock.end())
std::cout << "\nModel " << model << ": " << fnd->second.desc << " for $" << fnd->second.price << '\n';
else
std::cout << "\nThe model " << model << " is not available\n";
}
| |