FAQ: Conditionals & Logic - Review

Hello, @mohammedomer76019885, and welcome to the forums.

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…

Thanks for the suggestion! I will implement this and see how it looks like.

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?

Right. You’d have to use std::string.

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. :rofl: :rofl: :rofl:

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

switch(planetno) {

case 1 :
rgravity = 0.38;
std::cout << “Planet Chosen = Mercury\n”;
break;
case 2 :
rgravity = 0.91;
std::cout << “Planet Chosen = Venus\n”;
break;
case 3 :
rgravity = 0.38;
std::cout << “Planet Chosen = Mars\n”;
break;
case 4 :
rgravity = 2.34;
std::cout << “Planet Chosen = Jupiter\n”;
break;
case 5 :
rgravity = 1.06;
std::cout << “Planet Chosen = Saturn\n”;
break;
case 6 :
rgravity = 0.92;
std::cout << “Planet Chosen = Uranus\n”;
break;
case 7 :
rgravity = 1.19;
std::cout << “Planet Chosen = Neptune\n”;
break;
default :
std::cout << “Error: Planet does not exist\n”;
system(“pause”);
return 0;
}

//Calculate relative weight

rweight = earthweight*rgravity;
std::cout << "Your relative weight on this planet will be " << rweight << “kgs.\n”;

system(“pause”);
}

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”;
}

}

I think he/she declared new variables named “rg”, which stand for “relative gravity”.

Thank you very much. I actually did not understand until the review part and thanks to you I understood 80% of what this code means. Happy coding :blush:

Ok, Im totally lost on this challenge. please help!

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.

you’re using variables that you haven’t defined. rGVenus, rGMars aren’t defined, they should be like

std::cout << "Your rG on Venus is " << Eweight * 0.91 << "\n";

maybe having undefined variables messes up the calculation of mercury. also you haven’t coded the formulas for all the planets after mercury.

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; } }
1 Like

This is my code. It will not prompt me for my weight… this is almost identical to the answer, so why does it not work?

#include <iostream>

int main() {
  
  double weight;
  int x;

std::cout << "Please enter your current Earth weight: ";
std::cin >> weight;

std::cout << "I have info for following planets: ";
std::cout << "1. Mercury  2. Venus  3. Mars  4. Jupiter  5. Saturn 6.  Uranus  7. Neptune\n";

std::cout << "Which planet will you choose: ";
std::cin >> x;

  if (x == 1) {
    weight = weight * 0.38;
  }
  else if (x == 2){
    weight = weight * 0.91;
  }
  else if (x == 3){
    weight = weight * 0.38;
  }
  else if (x == 4){
    weight = weight * 2.34;
  }
  else if (x == 5){
    weight = weight * 1.06; 
  }
  else if (x == 6){
    weight = weight * 0.92;
  }
  else if (x == 7){
    weight = weight * 1.19;
  }

  

std::cout << "Your weight is: " << weight << "\n";
  }

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.

1 Like

No. I even copied the code, repasted, ran and tried again and nothing. It must be the compiler. Not the first time something like that has happened.

thanks for checking it out though at least I know it works.

I’ve seen some erratic behavior on occasion as well. What I would suggest is that you:

  1. Save the code you posted (Click Run).
  2. Delete the a.out file.
  3. Reload the page.
  4. Compile your code.
  5. Execute the new a.out file.