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
|
#include <iostream>
#include <string>
/*
Storing arbitrary data - things that don't have much to do with one another - in an array, is somewhat strange, and it encourages bad design choices.
Typically, in your case, you would write a class to contain the data, and then pass an instance of that class to a function for modification.
But, here's my example, with an array.
Things I did differently:
You chose to store the user input in an array, while I chose to store constant planetary properties, such as gravitational acceleration, names and distances.
However, this design choice makes a switch control structure useless.
*/
void printTravelTime(unsigned short speed, unsigned long int distance);
double calculateWeight(unsigned short initialWeight, double modifier);
int main() {
std::string name = "";
unsigned short weight = 0;
unsigned short speed = 0;
unsigned short selection = 0;
const unsigned short num_planets = 8;
std::cout << "Enter your name:\t";
std::getline(std::cin, name);
std::cout << "Enter your weight (lbs): ";
while (true) {
std::cin >> weight;
if (weight > 0) {
break;
}
std::cout << "Invalid input, try again:\t";
}
std::cout << "Enter your speed(MPH):\t";
while (true) {
std::cin >> speed;
if (speed > 0) {
break;
}
std::cout << "Invalid input, try again:\t";
}
system("cls");
std::cout << "Here is a list of the planets you can visit." << std::endl;
std::cout << "(1)Mercury\n(2)Venus\n(3)Earth\n(4)Mars\n(5)Jupiter\n(6)Saturn\n(7)Uranus\n(8)Neptune" << std::endl;
std::cout << "Enter the corresponding number of the planet you'd like to visit:\t";
while (true) {
std::cin >> selection;
if (selection >= 1 && selection <= num_planets) {
break;
}
std::cout << "Invalid input, try again:\t";
}
//Here's where I would use classes
std::string planet_names[num_planets] = {
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune"
};
double planet_accelerations[num_planets] = {
0.378,//Mercury
0.907,//Venus
1.000,//Earth
0.377,//Mars
2.360,//Jupiter
0.916,//Saturn
0.889,//Uranus
1.120//Neptune
};
long int planet_distances[num_planets] = {
57000000,//Mercury
26000000,//Venus
0,//Earth
48000000,//Mars
390000000,//Jupiter
793000000,//Saturn
1689000000,//Uranus
2700000000//Neptune
};
system("cls");
std::cout << "Name:\t\t" << name << std::endl;
std::cout << "Destination:\t" << planet_names[selection - 1] << std::endl;
std::cout << "Weight on Earth: " << weight << std::endl;
std::cout << "Weight on " << planet_names[selection - 1] << ": " << calculateWeight(weight, planet_accelerations[selection - 1]) << std::endl;
printTravelTime(speed, planet_distances[selection - 1]);
std::cin.ignore();
std::cin.get();
return 0;
}
void printTravelTime(unsigned short speed, unsigned long int distance) {
unsigned long int hours = distance / speed;
unsigned long int days = hours / 24;
hours -= (days * 24);
unsigned long int years = days / 365;
days -= (years * 365);
std::cout << "Travel time:\t" << years << " years, " << days << " days and " << hours << " hours." << std::endl;
}
double calculateWeight(unsigned short initialWeight, double modifier) {
return (initialWeight * modifier);
}
|