FAQ: Conditionals & Logic - Review

This community-built FAQ covers the “Review” exercise from the lesson “Conditionals & Logic”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I did the optional exercise for the review section.
I used a switch statement to calculate the weight on different planets, as seen below(and it works). However when I want to add the statement "your weight in this planet is " std::cout << "Your weight in this planet is " +(weight * rg2); I get an error.
The error is: "invalid operand of char + binary operator
Why is that?:thinking:

int main() {
  double weight;
  std::cout << "What is your earth weight\n";
  std::cin >> weight;
  int planet;
  std::cout << "Enter number (1-6) for planet to fight on\n";
  std::cin >> planet;
  switch (planet){
    case 1:
      {double rg1 = 0.78;
      std::cout << (weight * rg1);}
      break;
    case 2:
      {double rg2 = 0.39;
      std::cout << (weight * rg2);}
      break;
    case 3:
      {double rg3 = 2.65;
      std::cout << (weight * rg3);}
      break;
    case 4:
      {double rg4 = 1.17;
      std::cout << (weight * rg4);}
      break;
    case 5:
      {double rg5 = 1.05;
      std::cout << (weight * rg5);}
      break;
     case 6:
      {double rg6 = 1.23;
      std::cout << (weight *rg6);}
      break;
  }

2 Likes

Solved
I got it!!!
I was concatenating using “+” instead of " <<" for the standard output!!!
Now my output is how I wanted it “your weight on this planet is ***”

1 Like

It took me way longer than it should have to figure out why declaring a string failed when compiling…
Strings were mentioned in the earlier lessons, but no examples or details on how to use them were given.
After some googling I found out you have to have “using namespace std;” or use std::string instead of just string when declaring.

Once I finally figured that out, this was my final program.

#include <iostream>
using namespace std;

int main() {
 
  double earthWeight;
  int planetNumber;
  double planetWeight;
  string planetName;
  
  std::cout << "Please enter your weight on Earth: ";
  std::cin >> earthWeight;
  
  std::cout << "Please select a planet from the following list:\n\n";
  
  std::cout << "   1: Venus\n";
  std::cout << "   2: Mars\n";
  std::cout << "   3: Jupiter\n";
  std::cout << "   4: Saturn\n";
  std::cout << "   5: Uranus\n";
  std::cout << "   6: Neptune\n\n";
  
  std::cout << "Planet Number: "; 
  std::cin >> planetNumber;  
  
  switch (planetNumber){
    case 1 :
      planetWeight = earthWeight * 0.78;
      planetName = "Venus";
      break;
    case 2 :
      planetWeight = earthWeight * 0.39;
      planetName = "Mars";
      break;
    case 3 :
      planetWeight = earthWeight * 2.65;
      planetName = "Jupiter";
      break;
    case 4 :
      planetWeight = earthWeight * 1.17;
      planetName = "Saturn";
      break;
    case 5 :
      planetWeight = earthWeight * 1.05;
      planetName = "Uranus";
      break;
    case 6 :
      planetWeight = earthWeight * 1.23;
      planetName = "Neptune";
      break;
    default :
      planetName = "Unknown";
      break;
  }
  
  if (planetName == "Unknown"){
    std::cout << "Invalid planet input.\n";
  }else{
    std::cout << "Your weight on " << planetName << " would be " << planetWeight << "\n";
  }
  
}
2 Likes

//My final program

#include

int main() {
double w = 150;
int p = 8;
std::cout << “What is your earth weight?\n”;
std::cin >> w;
std::cout << “Which plant number will you fight on?\n”;
std::cout << “1. Venus 2. Mars 3. Jupiter 4. Saturn 5. Uranus 6. Neptune \n\n”;
std::cin >> p;

switch § {
case 1 :
std::cout << "Your weight is "<< w0.78 <<“lbs,\n”;
break;
case 2 :
std::cout << "Your weight is " << w
0.39 <<“lbs,\n”;
break;
case 3 :
std::cout << "Your weight is " << w2.65 <<“lbs,\n”;
break;
case 4 :
std::cout << "Your weight is "<< w
1.17 <<“lbs,\n”;
break;
default :
std::cout << "Your weight is "<< w*1.05 <<“lbs,\n”;
break;
}

}

Here’s my solution to the exercise! :smiley: it works

#include

int main() {
double weight;
std::cout << “Enter your earth weight\n”;
std::cin >> weight;

int planet;
std::cout << “Please, choose a number for the planet you want to fight on\n”;
std::cout << " 1 = Venus\n";
std::cout << " 2 = Mars\n";
std::cout << " 3 = Jupiter\n";
std::cout << " 4 = Saturn\n";
std::cout << " 5 = Uranus\n";
std::cout << " 6 = Neptune\n";
std::cin >> planet;
double v1=0.78;
double m2=0.39;
double j3=2.65;
double s4=1.17;
double u5=1.05;
double n6=1.23;

switch (planet) {
case 1:
std::cout << "Your weight in Venus is " << (weightv1) <<"\n";
break;
case 2:
std::cout << "Your weight in Mars is " << (weight
m2) << “\n”;
break;
case 3:
std::cout << "your weight in Jupiter is " << (weightj3) << “\n”;
break;
case 4:
std::cout << “Your weight in Saturn is” << (weight
s4) << “\n”;
break;
case 5:
std::cout << “Your weight in Uranus is” << (weightu5) << “\n”;
break;
case 6:
std::cout << “Your weight in Neptune is” << (weight
n6) << “\n”;
break;
default:
std::cout << “Error in numbers\n”;

  }

}

Here’s mine pretty much as those who choose a similar switch-case solution. Have a good one!

#include
#include <stdlib.h>

int main() {
double weight;
int planet;
std::cout << “Hi champ! what’s your weight in Earth?\n”;
std::cin >> weight;
std::cout << “On which planet is your next fight?\n 1)Venus 2)Mars 3)Jupiter\n 4)Saturn 5)Uranus 6)Neptune\n”;
std::cin >> planet;
// I choose to have a more clean structure with a switch-case flow instead of the if-else if-else
// (which is completely right as an option)
switch(planet) {
case 1: weight = weight / 0.78; std::cout << “Your weight in Venus is:\n” << weight << " kg\n";
break;
case 2: weight = weight / 0.39; std::cout << “Your weight in Mars is:\n” << weight << " kg\n";
break;
case 3: weight = weight * 2.65; std::cout << “Your weight in Jupiter is:\n” << weight << " kg\n";
break;
case 4: weight = weight * 1.17; std::cout << “Your weight in Saturn is:\n” << weight << " kg\n";
break;
case 5: weight = weight * 1.05; std::cout << “Your weight in Uranus is:\n” << weight << " kg\n";
break;
case 6: weight = weight * 1.23; std::cout << “Your weight in Neptune is:\n” << weight << " kg\n";
break;
}
}

1 Like

This is what I used:

#include <iostream>
#include <thread>
#include <chrono>

int main() {
int weight;
int planet;
std::cout << "what is your earth weight? ";
std::cin >> weight;
std::cout << "which planet do you want to fight in? ";
std::cin >> planet;
switch (planet) {
  case 1:
std::cout << planet / 0.78 << std::endl;
    break;
  case 2:
std::cout << planet / 0.39 << std::endl;
    break;
  case 3:
std::cout << planet / 2.65<< std::endl;
    break;
  case 4:
std::cout << planet / 1.17 << std::endl;
    break;
  case 5:
std::cout << planet / 1.05 << std::endl;
    break;
  case 6:
std::cout << planet / 1.23 << std::endl;
    break;
  default:
    std::cout << "Invalid\n";
    break;
  
}

}```
also please make sure to tell us what arithmetic to use :D

Guys,why the he** is no (return 0;) needed at the end?!
Cheers for the replies!

Tried to do a fancy work… but I failed…
Here is my code:
#include

int main() {
double EW;
int num;
cart PN;
double FW;
wait
//Variables I defined
std::cout << “Hello warrior!\n”;
std::cout << “This is a programme to help you with calculate the weight of you in different planet only if you answer the following question.\n”;
//Greatings and intros.
std::cout << “1.What is your weight on EARTH?\n”;
std::cin >> EW;
if (EW <= 0){
std::cout >> “Please renter your weight.\n”;
return;
//In case someone enter a impossible number of weight.
} else {
std::cout >> “Great!”;
}
std::cout << “2.Which planet you want to fight on? Please enter the number of the planet. Here is the number of each planet:\n\n”;
std::cout << " 1. Venus 2. Mars 3. Jupiter\n";
std::cout << " 4. Saturn 5. Uranus 6. Neptune\n\n";
//A tip.
std::cin >> num;
//Enter the number of planet.
switch (num) {
case 1:
FW == EW* 0.78;
PN == “Venus”;
case 2:
FW == EW* 0.39;
PN == “Mars”;
case 3:
FW == EW* 2.56;
PM == “Jupiter”;
case 4:
FW == EW* 1.17;
PN == “Saturn”;
case 5:
FW == EW* 1.05;
PN == “Uranus”;
case 6:
FW == EW* 1.23;
PN == “Neptune”;
default:
std::cout << “We only know about thouse 6 planets right now.”;
return;
//In case user enter a number which is not 1, 2, 3, 4, 5, or 6.
}
std::cout << “So your wight on”;
std::cout >> PN;
std::cout << “is:”;
std::cout >> FW;
std::cout << “\n THX for using this programme!GH & HF!”;
//Final statement
}
I would appreciate if someone can help me!

Compiled and executed using
g++ space.cpp
./a.out
And typed the following code, however the code didn’t run and code academy let me pass?
Any help would be greatly appreciated:

int main() {
double eweight;
std::cout <<“What is your earth weight?\n”;
std::cout <<"My earth weight is ";
std::cin >> eweight;

int num;
std::cout <<“What number planet do you want to fight on?\n”;
/*#, Planet, Relative gravity
1 Venus 0.78
2 Mars 0.39
3 Jupiter 2.65
4 Saturn 1.17
5 Uranus 1.05
6 Neptue 1.23
*/
std::cin >> num;
double pweight;
switch(num){
case 1:
pweight=eweight * 0.78;
break;
case 2:
pweight=eweight * 0.39;
break;
case 3:
pweight=eweight * 2.65;
break;
case 4:
pweight=eweight * 1.17;
break;
case 5:
pweight=eweight * 1.05;
break;
case 6:
pweight=eweight * 1.23;
break;
default:
std::cout <<“Invalid number please try again\n”;
}
std::cout <<“Your planet weight is “<<pweight<<”\n”;

}

Since the main function is a interger(int). Therefore you must return a interger. So imagine if the main function was a string (int main > string main). You have to return a string like

return "hello"

or if it was a bool function (int main > bool main). Then the return would be either true or false

return true

cheers mate!!
have still a lot to cover :confused:

I don’t know why it’s have a lot of “\n” in your HINT
Can you explain to me why is it?
Thanks a lot :smile:\

Here is my solution. I decided to use a switch as well - seems like less work?

#include <iostream>

int main() {
  int earth_weight;
  int planet;
  double planetary_weight;
    
  std::cout << "Enter your earth weight: \n";
  std::cin >> earth_weight;
  
  std::cout << "I have information on the following planets: \n";
  std::cout << "1.Venus 2.Mars 3.Jupiter \n";
  std::cout << "4.Saturn 5.Uranus 6.Neptune \n";
  std::cout << "Please make your selection: \n";
  std::cin >> planet; 
  
  switch (planet) {
    case 1:
      std::cout << "Venus\n";
      planetary_weight = earth_weight * .78;
        break;
    case 2:
      std::cout << "Mars\n";
      planetary_weight = earth_weight * .39;
        break;
    case 3:
      std::cout << "Jupiter\n";
      planetary_weight = earth_weight * 2.65;
        break;
    case 4:
      std::cout << "Saturn\n";
      planetary_weight = earth_weight * 1.17;
        break;
     case 5:
      std::cout << "Uranus\n";
      planetary_weight = earth_weight * 1.05;
        break;
      case 6:
      std::cout << "Neptune\n";
      planetary_weight = earth_weight * 1.23;
        break;
      default: 
      std::cout << "Invalid input\n";
  }
  
  std::cout << "Your weight on this planet is " << planetary_weight << "\n";
}
2 Likes

I assumed that he already knew what planet was which based off the number. This is what i did

#include <iostream>
using namespace std;
// W = M x G
int main() {
  
  float earth_weight, mass, gravity, weight;
  short planet;
  
  cout<<"What is your earth weight?\n";
  cin>> earth_weight;
  
  cout<<"Enter the number of the planet that you are fighting in? \n";
  cin>>planet;
  //calculating his mass from his earth weight
  mass = earth_weight/0.98;
  //calculating his weight on the planet he inputed
  switch(planet){
    case 1 :
      weight = mass * 0.78;
      break;
    case 2:
      weight = mass * 0.39;
      break;
    case 3:
      weight = mass * 2.65;
      break;
    case 4:
      weight = mass * 1.17;
      break;
    case 5:
      weight = mass * 1.05;
      break;
    case 6:
      weight = mass * 1.23;
      break;
  }
  cout<<"Your weight on planet #"<<planet<<"is "<<weight<<endl;
   
}

Hi Netpro,
Thanks for sharing your work! Looks neat.

Yeah, I encountered the same problem previously, and learnt that “string” is not a variable that is really at the bottom of the heart of C++ like char, int, double, etc. Rather, string is within std, the standard library, thus it is necessary to add the “std::” bit. I read it’s because string is essentially a ‘string’ of characters (chars).

Anyway just wanted to add few words from my recent learning as well. Happy coding.

Hi all,

Here is my piece of work. I made an extra effort to deal with naughty users, who will put in negative values as weight, etc. haha.
This can also be found from: https://github.com/haratzj/space/blob/master/space.cpp.

Cheers, Happy coding.

//This is an app that converts one's earth weight to a chosen planet weight

#include <iostream>

int main (){
    int planet_num, whilecheck=0, ifcheck=0;
    double earth_weight, venus=0.78, mars=0.39, jupiter=2.65, saturn=1.17, uranus=1.05, neptune=1.23;
    std::cout << "What is your weight on Earth?: ";
    std::cin >> earth_weight;

    // This while loop checks the earth_weight input and make sure its not negative. Giving 5 chances.
    while (earth_weight < 0 && whilecheck < 5) {
        std::cout << "You can't have negative value here. Please re-enter your weight : ";
        std::cin >> earth_weight;
        whilecheck = whilecheck +1;
    }
    // If the user has put in a negative value for more than 5 times, the code is forced to break, by returning 0.
    if (whilecheck > 4) {
        std::cout << "Stop putting in negatvie number!\n";
        return 0;
    }
    std::cout << "Also, on what planet do you want to fight?\n" << "1-Venus, 2-Mars, 3-Jupiter, 4-Saturn, 5-Uranus, 6-Neptune: ";
    std::cin >> planet_num;

    // Similar input validity check
    while (planet_num != 1 && planet_num != 2 && planet_num != 3 && planet_num != 4 && planet_num != 5 && planet_num !=6 && ifcheck < 5) {
        std::cout << "Please choose a valid option : ";
        std::cin >> planet_num;
        ifcheck = ifcheck +1;
    }
    if (whilecheck > 4 || ifcheck >4) {
        std::cout << "Stop putting in invalid inputs!\n";
        return 0;
    }

    switch(planet_num){
        case 1:
        std::cout << "You have chosen Venus! " << "You will weigh " << earth_weight*venus << "kg there!\n";
        break;
        case 2:
        std::cout << "You have chosen Mars! " << "You will weigh " << earth_weight*mars << "kg there!\n";
        break;
        case 3:
        std::cout << "You have chosen Jupiter! " << "You will weigh " << earth_weight*jupiter << "kg there!\n";
        break;
        case 4:
        std::cout << "You have chosen Saturn! " << "You will weigh " << earth_weight*saturn << "kg there!\n";
        break;
        case 5:
        std::cout << "You have chosen Uranus! " << "You will weigh " << earth_weight*uranus << "kg there!\n";
        break;
        case 6:
        std::cout << "You have chosen Neptune! " << "You will weigh " << earth_weight*neptune << "kg there!\n";
        break;
        default: //default does not have 'break' as it really doesn't matter. but it may be a good practise to include one?
        std::cout << "Sorry, something went wrong... Stay on the Earth maybe...\n";
    }
}

I added on to this.

#include <algorithm>
#include <locale>
#include <iostream>

using namespace std;

int main() {
  //It should ask him what his earth weight is.
  cout << "How much do you weigh on Earth?\n";
  double weight;
  cin >> weight;
  
  //Ask him to enter a the planet he wants to fight on.
  string planet;
  cout << "Which planet do you want to fight on?\n";
  cin >> planet;
  
  // convert string to back to lower case
  for_each(planet.begin(), planet.end(), [](char & c) {
	  c = tolower(c);
  });  
  
  //It should then compute his weight on the destination planet.
    if (planet == "venus")
      weight *= 0.78;
    else if (planet == "mars")
      weight *= 0.39;
    else if (planet == "jupiter")
      weight *= 2.65;
    else if (planet == "saturn") 
      weight *= 1.17;
    else 
      cout << "Planet unknown.\n";
  
  cout << "Your weight will be " << weight << ".\n";
}
1 Like

I struggled a little at the start with the switch, so i copied my code into a notebook and tried re-writing with if statements. Which did work, but i was determined not to move on in the lessons until i figured what i was doing wrong with the switch statements, in the end it was me putting the wrong variables into the switch statements… so here’s the final code i did, and it works! :slight_smile:

#include <iostream>

int main() {
  
  double earth;
  int planet;
  
  std::cout << "Please enter your earth weight: ";
  std::cin >> earth;

  std::cout << "Please Chose Your Planet You Want To Visit!\n";
  std::cout << "1.Venus 2.Mars 3.Jupiter 4.Saturn 5.Uranus 6.Neptune\n";
  std::cin >> planet;
  
  switch (planet) {
    case 1:
      earth = planet * 0.78;
      break;
    case 2:
      earth = planet * 0.39;
      break;
    case 3:
      earth = planet * 2.65;
      break;
    case 4:
      earth = planet * 1.17;
      break;
    case 5:
      earth = planet * 1.05;
      break;
    case 6:
      earth = planet * 1.23;
      break;
    default:
      std::cout << "Invalid\n";
      break;
      
  }
  
  std::cout << "Your weight will be: " << earth << "\n";
  
  
  
  
  
  
  
  
  
}