#include #include #include #include #include //includes using namespace std; int main() { typedef map map_t; typedef map_t::value_type pair_t; //another name for pair const pair_t a[] = { pair_t("Mercury", .27), pair_t("Venus", .85), pair_t("Earth", 1.00), pair_t("Mars", .38), pair_t("Jupiter", 2.33), pair_t("Saturn", .92), pair_t("Uranus", .85), pair_t("Neptune", 1.12), pair_t("Pluto", .44) }; const size_t n = sizeof a / sizeof a[0]; const map_t gravity(a, a + n); cout << "How many pounds do you weigh on Earth? "; double weight; if (!(cin >> weight)) { return EXIT_FAILURE; } for (;;) { cout << "Type name of planet, or q to quit, and press RETURN: "; string name; if (!(cin >> name)) { if (cin.eof()) { break; } return EXIT_FAILURE; } if (name == "q") { break; } const map_t::const_iterator it = gravity.find(name); if (it == gravity.end()) { cout << "No planet is named \"" << name << "\".\n"; } else { cout << "You would weigh " << weight * it->second << " pounds on " << name << ".\n"; } } cout << "\n"; cout << setprecision(2) << fixed; for (map_t::const_iterator it = gravity.begin(); it != gravity.end(); ++it) { cout << left << setw(7) << it->first << right << " " << setw(4) << it->second << "\n"; } return EXIT_SUCCESS; }