Conditional and Logic Lesson (Planets)

I am doing the Conditional and Logic lesson. I’m on the review with the planets. I tried to do it by myself first. When I got stuck I looked at the hint and I was doing it all wrong. I was putting each planet in as a variable. In the hint they only put “weight” and “x” as variables. I know I was over-complicating it, but why would I not put each planet as variables? There’s a disconnect there that I’m not getting. Also, I don’t fully understand when to use what ‘type’; I guess. I understand that ‘int’ is whole numbers and ‘double’ is decimals, and so on. With that same lesson, I don’t understand why “weight” would be a double, and “x” would be int and not char? Is it because the user is going to put a number for “x”? If so, why would “weight” be double instead of int? I’m sorry if these questions are confusing. I am not sure how else to ask them. I appreciate your help.

Here is the link:
https://www.codecademy.com/courses/learn-c-plus-plus/lessons/cpp-conditionals-and-logic/exercises/review

Hi, @cinemadrue, welcome to the Codecademy Forums!

There are a few ways to do this exercise. Ideally, you would accomplish the task using either an if-else statement or a switch statement. If you were doing one of those prior to looking at the hint, chances are your program would have accomplished the same thing, just a different way.

As for the variable types, the reason the hint uses double weight and int x is partially due to the instructions, and partially due to the math necessary for this program.

The prompt specifically tells you to ask the boxer the number of the planet he wants to fight on:
image
This is why the variable x is an int. Of course, you could store this variable under a different name, such as planet, or you could even ask for the name of the planet. In that case you wouldn’t store the variable as an integer, but you could still achieve the same outcome.

The reason weight is a double is because in the hint they reassign the value of weight after multiplying the initial weight by the other planet’s relative gravity and it needs to be a double to store this new value. If you wanted, you could always store the original weight in an integer variable (maybe earth_weight) and the later value in a double variable (such as planet_weight).

Hope this clears up some of the confusion. Happy coding!

2 Likes

Yes that does help. Thank you again for your help.