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 82 83 84 85 86
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
enum PlanetType { Bad = -1, Mercury = 0, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, MAXPLAN };
struct Names {
string name;
double conv {};
};
const Names planets[10] {{"Mercury", 0.4155}, {"Venus", 0.8975}, {"Earth", 1}, {"Moon", 0.166}, {"Mars", 0.3507}, {"Jupiter", 2.5375}, {"Saturn", 1.067},
{"Uranus", 0.8947}, {"Neptune", 1.1794}, {"Pluto", 0.0899}};
// Display the planets
void disNames()
{
cout << "[ ";
for (const auto& p : planets)
cout << p.name << ' ';
cout << "]";
}
void GetUserInput(double& weight, string& planet)
{
// Make sure only number entered
while ((cout << "Enter You Weight: ") && !(cin >> weight)) {
cout << "Invalid number\n";
cin.clear();
cin.ignore(1000, '\n');
}
cout << "Enter Name of You wish to Check ";
disNames();
cout << " : ";
cin >> planet;
cin.ignore(1000, '\n');
// Convert input to Capitalised lower case
if (!planet.empty())
for (auto& ch : planet)
ch = (char)std::tolower(ch);
planet.front() = (char)std::toupper(planet.front());
}
void OutputWeight(PlanetType p, double weight)
{
cout << "Your weight on planet " << planets[p].name << " would be " << weight * planets[p].conv << '\n';
}
PlanetType fndPlanet(const string& planet)
{
for (PlanetType i = Mercury; i < MAXPLAN; i = (PlanetType)(i + 1)) // To increment an enum type
if (planets[i].name == planet)
return i;
return Bad;
}
int main()
{
char ans {};
do {
double weight {};
string planet;
GetUserInput(weight, planet);
if (auto p = fndPlanet(planet); p != Bad)
OutputWeight(p, weight);
else {
cout << "Error : planet name should be one of ";
disNames();
cout << '\n';
}
cout << "Again? (y/n): ";
cin >> ans;
cin.ignore(1000, '\n');
} while (ans == 'y' || ans == 'Y');
}
|