FAQ: Conditionals: Lesson - Review

This community-built FAQ covers the “Review” exercise from the lesson “Conditionals: Lesson”.

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!
You can also find further discussion and get answers to your questions over in #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

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!

Came back now that the full course is released. scanf() is used in the review hint, but was formatting explained anywhere?

3 Likes

I’m thinking the same thing! I could be wrong but i’d swear there is no explanation (or not sufficiently explicit) for the use of scanf() prior to this exercise.

1 Like

This was a very strange lesson. I don’t remember being introduced to neither the scanf() function nor to the argument “&x”, which the hint suggested. Furthermore it says the lesson is optional, but I couldn’t click “up next” without solving the exercise, which meant I ended up just copying the hint.

3 Likes

Same here, follow carefully each previous lessons, scanf() was not approach.

1 Like

I encounter the same problem with scanf()
I solved it like this, have no idea if it is right

#include <stdio.h>

int main() {

double earth_weight = 50;
printf(“Earth weight: %f kg\n”, earth_weight);
int numberPlanet = 3;

float mercury1 = earth_weight0.38;
float venus2 = earth_weight
0.91;
float mars3 = earth_weight0.38;
float jupiter4 = earth_weight
2.34;
float saturn5 = earth_weight1.06;
float uranus6 = earth_weight
0.92;
float neptune7 = earth_weight*1.19;

{
if (numberPlanet = 1){
printf(“Mercury: %f\n”, mercury1);
}
else if (numberPlanet = 2){
printf(“Venus: %f\n”, venus2);
}
else if (numberPlanet = 3){
printf(“Mars: %f\n”, mars3);
}
else if (numberPlanet = 4){
printf(“Jupiter: %f\n”, jupiter4);
}
else if (numberPlanet = 5){
printf(“Saturn: %f\n”, saturn5);
}
else if (numberPlanet = 6){
printf(“Uranus: %f\n”, uranus6);
}
else if (numberPlanet = 7) {
printf(“Neptune: %f\n”, neptune7);
}
}

}

This is what I came up with. Not that pleased with its readability but seems to be working. Hope it helps.

#include <stdio.h>

int main() {

float earthWeight;
int planetNumber;
float currentWeight;

printf(“What is your weight on Earth?\n”);
scanf("%f", &earthWeight);

printf(“Enter the number of the planet you want to compete in:\n1 - Mercury\n2 - Venus\n3 - Mars\n4 - Jupiter\n5 - Saturn\n6 - Uranus\n7 - Neptune\n”);
scanf("%i", &planetNumber);
switch(planetNumber) {
case 1:
currentWeight = earthWeight * 0.38;
break;
case 2:
currentWeight = earthWeight * 0.91;
break;
case 3:
currentWeight = earthWeight * 0.38;
break;
case 4:
currentWeight = earthWeight * 2.34;
break;
case 5:
currentWeight = earthWeight * 1.06;
break;
case 6:
currentWeight = earthWeight * 0.92;
break;
case 7:
currentWeight = earthWeight * 1.19;
break;
default:
printf(“Unknown Planet:\n”);
break;
}

printf(“Your current weight is: %f\n”, currentWeight);
}

You don’t need currentWeight because you don’t need to save earthWeight, just use earthWeight = eatrhWeight * planet weight.
Also, you can printf weight inside switch case for example for case 7 :

case 7:
printf(“Your current weight on Neptune is: %f\n”, earthWeight * 1.19);
break;

It’s ok, but you don’t need to type weight for every planet. Just make variable weight and multiply it with planet constant.
For example :
if (numberPlanet = 1){
printf(“Mercury: %f\n”, weight0.38);
}
else if (numberPlanet = 2){
printf(“Venus: %f\n”, weight
0.91);
}
etc.

1 Like

I don’t know why you don’t include return value at the end of the function because main function is int ?
Here is my solution :

#include <stdio.h>

int main() {
double weight;
int planet;
printf(“Enter the weight: “);
scanf(”%lf”,&weight);
printf(“Enter the planet from 1-7: “);
scanf(”%d”,&planet);

switch (planet) {
case 1:
printf(“Your weight on Mercury is %.2lf\n”,weight0.38);
break;
case 2:
printf(“Your weight on Venus is %.2lf\n”,weight
0.91);
break;
case 3:
printf(“Your weight on Mars is %.2lf\n”,weight0.38);
break;
case 4:
printf(“Your weight on Jupiter is %.2lf\n”,weight
2.34);
break;
case 5:
printf(“Your weight on Saturn is %.2lf\n”,weight1.06);
break;
case 6:
printf(“Your weight on Uranus is %.2lf\n”,weight
0.92);
break;
case 7:
printf(“Your weight on Neptune is %.2lf\n”,weight*1.19);
break;
default:
printf(“Unknown planet\n”);
break;
}
return 0;
}

As the others mentioned, this task is impossible for an inexperienced programmer without using the hints, because scanf() and the & operator are never explained. Fortunately I’m fairly experienced with C so I thought I might try to explain things a little.
The solution for this exercise looks something like this:

#include <stdio.h>

int main() {
  float weight = 0;
  int planet = 0;

  printf("What is your Earth weight?\n> ");
  scanf("%f", &weight);

  printf("Which planet would you like to fight on?\n> ");
  scanf("%d", &planet);

  switch(planet) {
    case 1:
      weight *= 0.38;
      break;
    case 2:
      weight *= 0.91;
      break;
    case 3:
      weight *= 0.38;
      break;
    case 4:
      weight *= 2.34;
      break;
    case 5:
      weight *= 1.06;
      break;
    case 6:
      weight *= 0.92;
      break;
    case 7:
      weight *= 1.19;
      break;
    default:
      printf("Error: Invalid planet supplied.\n");
  }
  printf("Interplanetary weight: %f\n", weight);
}

The important parts of the code are lines 7-11. This is how you use the aforementioned scanf() function. It takes the general form scanf("<format string>", &<variable>). As we can see, the function takes two parameters.

The first parameter is what’s called a format string. It is a placeholder which includes a format specifier. We learned about these placeholders previously in the Variables lesson, but it was used in printf() rather than scanf(). The placeholder %f lets the compiler know that you’re reading in a floating-point (decimal) number, whereas %d lets the compiler know you’re reading in an integer (%i would signify the same thing).

The second parameter can be thought of as the variable you’re putting the input into. The & operator does have a specific effect here, but it’s tied to a more advanced topic called pointers. For now, just remember that this second parameter is just the name of the variable with & in front of it (for the curious: & retrieves the memory address of the variable as opposed to the number that is currently stored in the variable).

I hope this helps y’all understand a little bit better how to get user input. I don’t know why they introduced it here with no prior mention. I guess they wanted people to research stuff on their own, but in my experience it just demotivates me when I think I understood everything in a lesson and they throw something new at me. It makes me feel like I missed something when really I didn’t at all.

Don’t lose your drive to learn because of this. C is a difficult language to learn, but that just makes it all the more rewarding when your code compiles.

5 Likes

Thank you very much for explaining this and demonstrating its use in code!

How might we use a place-holder for a string? %s ?

This works elegantly when I also include the choice of planets.

#include <stdio.h>

int main()
{

double weight;
int planet;

printf(“What is your weight?: “);
scanf(”%lf”, &weight);
printf(“What is the number of the planet you want to fight on?: “);
scanf(”%d”, &planet);

switch(planet)
{
case 1:
weight *= 0.38;
break;

case 2:
weight *= 0.91;
break;

case 3:
weight *= 0.38;
break;

case 4:
weight *= 2.34;
break;

case 5:
weight *= 1.06;
break;

case 6:
weight *= 0.92;
break;

case 7:
weight *= 1.19;
break;

}

printf(“Your weight on planet %d is %lf\n”, planet, weight);

In exactly what sense is this a “review” of conditional statements in C when the exercise requires a firm grasp of material that hasn’t yet been introduced in the C course (viz., storing user input as a variable)?

I’m sure that the C course ranks pretty low on Codecademy’s list of priorities, but this “review” is indicative of the sorry state of QAQC for some of these courses.

//This the code i wrote using switch case and it is working fine
#include <stdio.h>

int main() {
double weight;
int x;

printf(“Please enter your current earth weight: “);
scanf(”%lf”, &weight);

printf(“\nI have information for the following planets:\n\n”);
printf(" 1. Mercury\n 2. Venus\n 3. Mars\n 4. Jupiter\n");
printf(" 5. Saturn\n 6. Uranus\n 7. Neptune\n\n");

printf(“Which planet are you visiting? “);
scanf(”%d”, &x);
switch(x){
case 1:printf(“Your weight: %f\n”,weight0.38);
break;
case 2:printf(“Your weight: %f\n”,weight
0.91);
break;
case 3:printf(“Your weight: %f\n”,weight0.38);
break;
case 4:printf(“Your weight: %f\n”,weight
2.34);
break;
case 5:printf(“Your weight: %f\n”,weight1.06);
break;
case 6:printf(“Your weight: %f\n”,weight
0.92);
break;
case 7:printf(“Your weight: %f\n”,weight*1.19);
break;
default:printf(“Invalid\n”);
break;

}

}

idk how to run the gcc what’s the command for it.