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 () 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 () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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.
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”, weight0.91);
}
etc.
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”,weight0.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”,weight2.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”,weight0.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.
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”,weight0.91);
break;
case 3:printf(“Your weight: %f\n”,weight0.38);
break;
case 4:printf(“Your weight: %f\n”,weight2.34);
break;
case 5:printf(“Your weight: %f\n”,weight1.06);
break;
case 6:printf(“Your weight: %f\n”,weight0.92);
break;
case 7:printf(“Your weight: %f\n”,weight*1.19);
break;
default:printf(“Invalid\n”);
break;