FAQ: Conditionals & Logic - Review


#1

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!


#2

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


#3

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 ***”


#4

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

#6

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

}


#7

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

  }

}


#8

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


#9

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

#10

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


#11

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!