-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanetaryWeight.cpp
157 lines (135 loc) · 3.45 KB
/
PlanetaryWeight.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
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
// Author: Roger Greer
// Purpose: This program calculates a users weight on a selected planet.
#include <iostream>
#include <string>
using namespace std;
enum Planets{MERCURY, VENUS, EARTH, MOON, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO};
const int Exit = -99; // Sentinel value
//Prototype Functions
void Instruct();
Planets planetSearch(string pSearch);
double calcWeight(Planets Name, double userWeight);
int main()
{
// Variables
double weight, pWeight;
string pName;
// User instructions
Instruct();
cout << "Enter your weight..." << endl;
cin >> weight;
while(weight != Exit)
{
cout << "Enter a planet..." << endl;
cin >> pName;
// Calculate user weight on planet
pWeight = calcWeight(planetSearch(pName), weight);
cout << "Your weight on " << pName << " is " << pWeight << " lbs." << endl;
cout << "Enter your weight..." << endl;
cin >> weight;
}
return 0;
}
// User instructions
void Instruct()
{
cout << "******************************************************************************" << endl;
cout << "*This program calculates a users weight on the selected planet. Please select*" << endl;
cout << "*from the following: MERCURY, VENUS, EARTH, MOON, MARS, JUPITER, SATURN, *" << endl;
cout << "*URANUS, NEPTUNE, PLUTO. Enter -99 for weight to Quit *" << endl;
cout << "******************************************************************************" << endl;
}
Planets planetSearch(string pSearch)
//PRE: The user has entered a planet name.
//POST: The user supplied planet name is validated, found in the enum, and returned to main function.
{
enum Planets name = MERCURY;
if(pSearch.length() < 4) // Shortest planet is MARS
cout << "Your selection is invalid. Please check your spelling." << endl;
switch(toupper(pSearch.at(0)))
{
case 'M':
switch(toupper(pSearch.at(1))) //Search at position one for M's
{
case 'E':
name = MERCURY;
break;
case 'A':
name = MARS;
break;
case 'O':
name = MOON;
break;
default:
cout << "Your selection is invalid. Please check your spelling." << endl;
break;
}
break;
case 'V':
name = VENUS;
break;
case 'E':
name = EARTH;
break;
case 'J':
name = JUPITER;
break;
case 'S':
name = SATURN;
case 'U':
name = URANUS;
break;
case 'N':
name = NEPTUNE;
break;
case 'P':
name = PLUTO;
default:
cout << "Your selection is invalid. Please check your spelling." << endl;
break;
}
return name;
}
// Calculates the users weight on the selected planet.
double calcWeight(Planets Name, double userWeight)
//PRE: The user has supplied a double value for weight and the planet name has
// been validated by the planetSearch function.
//POST: The users weight is calculated based on the planet selected and returned
// to the main function.
{
double planetWeight = 0;
switch(Name)
{
case MERCURY:
planetWeight = userWeight * 0.4155;
break;
case MARS:
planetWeight = userWeight * 0.3507;
break;
case MOON:
planetWeight = userWeight * 0.166;
break;
case VENUS:
planetWeight = userWeight * 0.8975;
break;
case EARTH:
planetWeight = userWeight * 1.0;
break;
case JUPITER:
planetWeight = userWeight * 2.5374;
break;
case SATURN:
planetWeight = userWeight * 1.0677;
break;
case URANUS:
planetWeight = userWeight * 0.8947;
break;
case NEPTUNE:
planetWeight = userWeight * 1.1794;
break;
case PLUTO:
planetWeight = userWeight * 0.0899;
break;
}
return planetWeight;
}