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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// enumerated type declaration to represent planets.
enum Planets {Mercury, Venus, Earth, Moon, Mars, Jupiter,
Saturn, Uranus, Neptune, Pluto};
// function prototypes
planetNames findPlanetName(string str);
double calcWeight(planetNames, double);
int main()
{
double weight;
double weightOnPlanet;
string planetName;
// Used to separate the lines cosmetically
string lineSeparator = "**********************************************************************************";
string spacer = " "; // Used to create space in program cosmetically.
cout << lineSeparator << endl;
cout << spacer << "What is your weight on different planets?" << spacer << endl;
cout << lineSeparator << endl << endl ;
cout << "Each planet have distinct features. For some of them, they have many rings around them," << endl;
cout << "and others rain diamonds. While these features are amazing, they are hard to relate to" << endl;
cout << "since they have little bearing with us here on Earth. However, there is one trait among" << endl;
cout << "each individual planet that we can relate to, and that is their surface's gravitational" << endl;
cout << "pull." << endl << endl;
cout << "While many factors are involved in their gravity, the main characteristic that defines" << endl;
cout << "it is their respective size compared to the others." << endl;
cout << "So, what makes this trait so we can relate to it? Well, the gravitational pull has certain" << endl;
cout << "effects on our bodies as well if we are on the surface, and the most notable one is our" << endl;
cout << "weight. This program allows you to enter your weight and the name of any planet (including" << endl;
cout << "the moon) and it will calculate your weight on the planet to show you how much this affects" << endl;
cout << "you." << endl << endl;
// Prompt user to enter his/her weight and the name of a planet
cout << "Please choose from any of these planets:" << endl;
cout << "Mercury" << endl;
cout << "Venus" << endl;
cout << "Earth" << endl;
cout << "Moon" << endl;
cout << "Mars" << endl;
cout << "Jupiter" << endl;
cout << "Saturn" << endl;
cout << "Uranus" << endl;
cout << "Neptune" << endl;
cout << "Pluto" << endl << endl;
cout << "Enter planet name here: ";
cin >> planetName;
cout << "Enter your weight: ";
cin >> weight;
// Call the function findPlanetName to get the name of the planet as enum value. Call the
// function calcWeight with planet name (enum value) and weight to get the weight on the planet.
weightOnPlanet = calcWeight (findPlanetName(planetName), weight);
// Display the planet name, and the weight on that planet
cout << "Weight on the " << planetName << " is " << weightOnPlanet << endl;
// pause the system
system("pause");
return 0;
}
//*******************************************************************************************
// Functions
//*******************************************************************************************
// The following function uses the string to find and return the planet name in the enumeration
// type that represents the planet names. It displays an error message if the planet is invalid.
// function to find the planet name if the planet name entered by the user is valid, then it
// returns the corresponding enum value
planetNames findPlanetName(string str)
{
enum planetNames name = Earth;
// Check if the planet name is valid or not based on length
if (str.length()<4)
{
cout << "Invalid planet name. Please check the spelling and try again." << endl;
system("pause");
exit (1);
}
// Switch case to find the name of the planet corresponding to the string
switch (toupper(str.at(0)))
{
case 'M':
switch (toupper(str.at(1)))
{
case 'E':
name = Mercury;
break;
case 'A':
name = Mars;
break;
case 'O':
name = Moon;
break;
default:
cout << "Invalid planet name. Please check the spelling and try again." << endl;
system("pause");
exit(1);
}
break;
case 'V':
name = Venus;
break;
case 'E':
name = Earth;
break;
case 'J':
name = Jupiter;
break;
case 'S':
name = Saturn;
break;
case 'U':
name = Uranus;
break;
case 'N':
name = Neptune;
break;
case 'P':
name = Pluto;
break;
default:
cout << "Invalid planet name. Please check the spelling and try again." << endl;
system("pause");
exit(1);
}
return name;
}
//*******************************************************************************************
// The following function calculates the weight of the person on the planet of his/her choice.
double calcWeight(planetNames planet, double weight)
{
double weightOnPlanet = 0;
// Switch case is used to identify the planet using enum values and calculate the weight
// on that planet
switch(planet)
{
case Mercury:
weightOnPlanet = weight * 0.4155;
break;
case Venus:
weightOnPlanet = weight * 0.8975;
break;
case Earth:
weightOnPlanet = weight;
break;
case Moon:
weightOnPlanet = weight * 0.166;
break;
case Mars:
weightOnPlanet = weight * 0.3507;
break;
case Jupiter:
weightOnPlanet = weight * 2.5374;
break;
case Saturn:
weightOnPlanet = weight * 0.8947;
break;
case Uranus:
weightOnPlanet = weight * 0.8947;
break;
case Neptune:
weightOnPlanet = weight * 1.1794;
break;
case Pluto;
weightOnPlanet = weight * 0.0899;
break;
}
// Return the weight on that planet
return weightOnPlanet;
}
|