You might consider how to ‘DRY’ your code a bit. Consider how you could simply have a single std::cout statement rather than repeating nearly the same line of code many times.
Hint
You could create a string variable, string planet = "";, assign the name of the planet to it inside your switch...case, and then print the results a single time: std::cout << "The planet chosen is " << planet << etc…
I just realized that string is not defined in the iostream library. Also, how does replacing the planet name with a variable shorten the length of the code?
If you only assign the name of the planet selected inside each case you could utilize a single std::cout statement after the entire switch...case structure to print the output. You wouldn’t need to print output inside each case. You’d have one std::cout statement instead of many.
Just adding insight on how I realized how a variable is not declared. Turns out, if you use an if-else set of blocks, you don’t have to declare the variable multiple times in each block.
You can only declare it at the begging of your code and that way it’s easier to follow, at least for me!
Hope it helps!
#include <iostream>
int main() {
float W;
float planetW;
int P;
std::cout << "¿What's your weight on earth?\n\n";
std::cin >> W;
std::cout << "\n\n¿What planet you want to fight on?\n\n";
std::cin >> P;
if( P==1 ){
planetW = W * 0.38;
}
else if( P==2 ){
planetW = W * 0.91;
}
else if ( P==3 ){
planetW = W * 0.38;
}
else if ( P==4 ){
planetW = W * 2.34;
}
else if ( P==5 ){
planetW = W * 1.06;
}
else if ( P==6 ){
planetW = W * 0.92;
}
else if ( P==7 ){
planetW = W * 1.19;
}
else{
std::cout << "\n\nCheck for the conversion table and select an available planet\n\n";
}
std::cout << "Your weight in the destination planet would be:" << planetW;
My contribution to this task. Main difference is that relative gravity is initialised in the cases and the calculation is performed at the end of the program.
#include
int main () {
//declare variables
double earthweight;
int planetno;
double rweight;
double rgravity;
//Prompt user to enter earthweight
std::cout << “Please enter your earth weight (kg) \n”;
std::cin >> earthweight;
//Let user choose planet
std::cout << “Please choose your desired planet by referincing the following table\n# Planet Relative Gravity\n1 Mercury 0.38\n2 Venus 0.91\n3 Mars 0.38\n4 Jupiter 2.34\n5 Saturn 1.06\n6 Uranus 0.92\n7 Neptune 1.19\n”;
std::cin >> planetno;
//switch case list of planets and relative gravities
Here is what I came up with. tried to keep it simple and self explanatory within the code itself.
As you can see I included the gravity information and initialised it within the IF statements.
Does this make it easy to read?
My thought was trying to keep it simple and as long as it got the correct results.
int main() {
double weight;
int planet;
std::cout << “Hello Little Mac\n” << “Please enter your Earth weight\n”;
std::cin >> weight;
std::cout << “Enter the number of the planet you wish to fight on.\n”;
std::cout << " 1. Mercury 2. Venus 3. Mars\n";
std::cout << “4. Jupiter 5. Saturn 6.Uranus 7.Neptune\n”;
std::cin >> planet;
if (planet == 1 )
{
std::cout << “Your weight on Mercury is\n” << weight * 0.38 <<“kg’s\n”;
}
else if (planet == 2)
{
std::cout << “Your weight on Venus is\n” << weight * 0.91 <<“kg’s\n”;
}
else if (planet == 3)
{
std::cout << “Your weight on Mars is\n” << weight * 0.38 <<“kg’s\n”;
}
else if (planet == 4)
{
std::cout << “Your weight on Jupiter is\n” << weight * 2.34 <<“kg’s\n”;
}
else if (planet == 5)
{
std::cout << “Your weight on Saturn is\n” << weight * 1.06 <<“kg’s\n”;
}
else if (planet == 6)
{
std::cout << “Your weight on Uranus is\n” << weight * 0.92 <<“kg’s\n”;
}
else if (planet == 7)
{
std::cout << “Your weight on Neptune is\n” << weight * 1.19 <<“kg’s\n”;
}
could someone explain me what am I doing wrong? Please only look at line 13. The rest of the code is using some variables I then later realized i don’t have to use. Also forget about the good practices I should have such as formatting and constant related data types.
Why is it giving me that number raised to the power of -310? Lol, I want to cry. In mercury given I weight 70 kg, the result should be approximately 26.6 kg.
this is my fully functional code for the review excercise. it works fine when i compile and run in a shell. I actually copied it to vs code and compiled it locally and again it works fine if i run it within cmd. however if i start the exe it opens a shell, goes through the steps but immediately closes once the code is finished robbing me of viewing the result of the calculation. I assume there’s some code to not autoclose the .exe or is this programm only intended to be run in a shell?
#include <iostream>
int main() {
double earthw;
int planet = 0;
//Prompt to enter weight on earth
std::cout << "Enter your weight on earth (kg): ";
std::cin >> earthw;
//Prompt to choose planet for conversion
std::cout << "Enter the number destination planet from the following list:\n1. Mercury\n2. Venus\n3. Mars\n4. Jupiter\n5. Saturn\n6. Uranus\n7. Neptune\n";
std::cin >> planet;
//Calculation of weight on destination
switch (planet) {
case 1:
std::cout << "Your weight on Mercury is " << 0.38 * earthw << ".\n";
break;
case 2:
std::cout << "Your weight on Venus is " << 0.91 * earthw << ".\n";
break;
case 3:
std::cout << "Your weight on Mars is " << 0.38 * earthw << ".\n";
break;
case 4:
std::cout << "Your weight on Jupiter is " << 2.34 * earthw << ".\n";
break;
case 5:
std::cout << "Your weight on Saturn is " << 1.06 * earthw << ".\n";
break;
case 6:
std::cout << "Your weight on Uranus is " << 0.92 * earthw << ".\n";
break;
case 7:
std::cout << "Your weight on Neptune is " << 1.19 * earthw << ".\n";
break;
}
}
I ran your code, and it seems to work as expected. Did you perhaps forget to click the Run button prior to compiling your code? In this case Run actually just saves your code. When you compile, it compiles whatever was saved last.
I used a switch for my solution. I think its a pretty clean and simple solution:
#include
int main() {
double weight_on_earth;
int p;
double weight;
std::cout << “What is your weight on earth?\n”;
std::cin >> weight_on_earth;
std::cout << “What planet are you fighting on?\n”;
std::cout << “Enter 1 for Mercury, 2 for Venus, \n3 for Mars, 4 for Jupiter, 5 for Saturn, \n6 for Uranus, or 7 for Neptune\n”;
std::cin >> p;
switch (p) {
case 1:
weight = weight_on_earth * 0.38;
std::cout << “Your weight on Mervury is " << weight << " pounds\n”;
break;
case 2:
weight = weight_on_earth * 0.91;
std::cout << “Your weight on Venus is " << weight << " pounds\n”;
break;
case 3:
weight = weight_on_earth * 0.38;
std::cout << “Your weight on Mars is " << weight << " pounds\n”;
break;
case 4:
weight = weight_on_earth * 2.34;
std::cout << “Your weight on Jupiter is " << weight << " pounds\n”;
break;
case 5:
weight = weight_on_earth * 1.06;
std::cout << “Your weight on Saturn is " << weight << " pounds\n”;
break;
case 6:
weight = weight_on_earth * 0.92;
std::cout << “Your weight on Uranus is " << weight << " pounds\n”;
break;
case 7:
weight = weight_on_earth * 1.19;
std::cout << “Your weight on Neptune is " << weight << " pounds\n”;
break;
default:
std::cout << “Invalid planet entry. Please pick a number between 1 and 7\n”;
break;
}
I had an issue because I used switch to process the number of the planet and then accidentally put a space before the “}” at the end of the function. facepalm I spent like 15 minutes writing the code and 30 minutes reeling over this f*** “}”