-
Notifications
You must be signed in to change notification settings - Fork 192
/
Planetary_Weights.cpp
97 lines (84 loc) · 2.29 KB
/
Planetary_Weights.cpp
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
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <cmath>
const double MERCURY_GRAVITY = 0.376;
const double VENUS_GRAVITY = 0.889;
const double MARS_GRAVITY = 0.378;
const double JUPITER_GRAVITY = 2.36;
const double SATURN_GRAVITY = 1.081;
const double URANUS_GRAVITY = 0.815;
const double NEPTUNE_GRAVITY = 1.14;
std::string capitalize(std::string str)
{
if (str.empty())
return str;
str[0] = std::toupper(str[0]);
return str;
}
bool isValidPlanet(const std::string& planet)
{
std::string validPlanets[] = { "Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
for (const std::string& validPlanet : validPlanets)
{
if (validPlanet == planet)
return true;
}
return false;
}
int main()
{
double earthWeight;
std::cout << "Enter a weight on Earth: ";
std::cin >> earthWeight;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string planet;
std::cout << "Enter a planet: ";
std::getline(std::cin, planet);
planet = capitalize(planet);
while (!isValidPlanet(planet))
{
if (planet == "Earth")
{
std::cout << "Please select a planet other than Earth." << std::endl;
}
else
{
std::cout << "Error: " << planet << " is not a planet." << std::endl;
}
std::cout << "Enter a planet: ";
std::getline(std::cin, planet);
planet = capitalize(planet);
}
double planetWeight;
if (planet == "Mercury")
{
planetWeight = earthWeight * MERCURY_GRAVITY;
}
else if (planet == "Venus")
{
planetWeight = earthWeight * VENUS_GRAVITY;
}
else if (planet == "Mars")
{
planetWeight = earthWeight * MARS_GRAVITY;
}
else if (planet == "Jupiter")
{
planetWeight = earthWeight * JUPITER_GRAVITY;
}
else if (planet == "Saturn")
{
planetWeight = earthWeight * SATURN_GRAVITY;
}
else if (planet == "Uranus")
{
planetWeight = earthWeight * URANUS_GRAVITY;
}
else
{
planetWeight = earthWeight * NEPTUNE_GRAVITY;
}
double roundedWeight = round(planetWeight * 100) / 100;
std::cout << "The equivalent weight on " << planet << ": " << roundedWeight << std::endl;
return 0;
}