#include #include #include #include #include //for map; include for class pair using namespace std; int main() { map gravity; //Default constructor constructs empty map. gravity["Mercury"] = .27; //gravity.operator[]("Mercury") = .27; gravity["Venus"] = .85; gravity["Earth"] = 1.00; gravity["Mars"] = .38; gravity["Jupiter"] = 2.33; gravity["Saturn"] = .92; gravity["Uranus"] = .85; gravity["Neptune"] = 1.12; gravity["Pluto"] = .44; cout << "How many pounds do you weigh on Earth? "; double weight; if (!(cin >> weight)) { //if (cin.operator>>(weight).operator!()) { 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 double factor = gravity[name]; //gravity.operator[](name); if (factor == 0.0) { cout << "No planet is named \"" << name << "\".\n"; } else { cout << "You would weigh " << weight * factor << " pounds on " << name << ".\n"; } } cout << "\n"; cout << setprecision(2) << fixed; //two digits to right of decimal point for (map::const_iterator it = gravity.begin(); it != gravity.end(); ++it) { //cout << (*it).first << " " << (*it).second << "\n"; cout << left << setw(7) << it->first << right << " " << setw(4) << it->second << "\n"; } return EXIT_SUCCESS; }