FAQ: Variables - Review

This community-built FAQ covers the “Review” exercise from the lesson “Variables”.

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 () 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.

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

Found a bug? Report it!

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

Excited that this exercise is here, and looking to contribute some ideas? Go here!

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

2 Likes

When is the reference guide for C++ variable going to come out?

My output is, "your bmi is: 'inf"

int main() {

double height, weight, bmi;
bmi = weight / (height * height);

std::cout << "Type in your height (m): ";
std::cin >> height;
std::cout << "Type in your weight (kg): ";
std::cin >> weight;
std::cout << "Your bmi is: " << bmi << "\n";

return 0;

}

When they literally say don’t forget to add a “\n” multiple times, and then they don’t

I got the same issue.
Did you fix it?

Please note that we assign values to the weight and height variables in these lines (by getting input from the console):

std::cin >> height;
std::cin >> weight;

But in the code provided by @thisisit8032542346 value of the bmi is calculated above these lines. So the computer is not able to use values provided by the user of our program because bmi is calculated earlier. To fix this problem we just need to move calculation of the bmi to the end of the script:

#include <iostream>

int main() {
// declare variables
double height, weight, bmi;

// get input and assign value to the variable height and weight
std::cout << "Type in your height (m): ";
std::cin >> height;
std::cout << "Type in your weight (kg): ";
std::cin >> weight;

// use inputed values to calculate bmi
bmi = weight / (height * height);
std::cout << "Your bmi is: " << bmi << "\n";

return 0;
}

Can anyone help me figure this out? My code looks almost exactly the same as previous successful versions, but it will only ask for one input and then give up. This is so frustrating.

Hello, @micro2271336966. Welcome to the forum!

Have you clicked Run since you added the code to get the user’s weight? Clicking Run saves your code. If you don’t click Run after each change in the code editor, you just re-compile the same code that was there before the previous time that you clicked Run.

1 Like

That did it, wow. Thank you very much for the fix, I feel so silly for not understanding that.

1 Like

No problem. I believe the instructions are a little unclear regarding the use of the Run button in the lessons where we have to compile the code. The button really should be labeled Save in these lessons.