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!
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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.
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.
It won’t print the other statements, just the first one. I also tried putting values into the variables and it wouldn’t calculate.
double height = 1.51, weight = 65.75, bmi;
Based o the lack of the command prompt - it looks like your program execution wasn’t finished, the program waits for the input from you. Click on the terminal (right pane), input the value for height (1.51) and press enter.
Hmm ok I got it. I thought the terminal printed out text. That’s why I got confused because I thought it print out : type in your height (m):, type in your weight (kg), and Your bmi, as the other lessons like Hello World.
There’s an issue with the final exercise with the BMI calculation. When I do the code exactly the way I’m supposed to:
#include <iostream>
int main () {
double height, weight, bmi;
std::cout << "Type in your height (m): ";
std::cin >> height;
std::cout << "Type in your weight (kg): ";
std::cin >> weight;
bmi = weight / (height * height);
std::cout << "Your BMI is " << bmi << "\n";
}
I get this really long error message about ; being expected before std::cout, but when i run it in g++ on my ubuntu laptop it works just fine, so there is clearly something wrong with the console in the exercise.
Hello, @calvy667, and welcome to the forums. If you’re sure you haven’t omitted any semicolons in your code in the Codecademy editor, please post a screenshot that includes your code as well as the error message in the console.
One possibility may be that you didn’t save your most recent edit in the editor prior to compiling, or haven’t compiled since your most recent saved edit. The Run button should really be labelled Save in this exercise.
i got question about the terminal after compiling and executing the code for the bmi exercise the terminal keeps giving me the same answer that was written as my weight ?
The output from my code is not what I expect or the last two exercises yet they are being marked as correct. For example the code below only outputs the following typing 1 as the first input
Type in your height (m): 1
dollar sign
It never asks for the weight. I cant understand why. As soon as I hit enter after typing in the number it goes to the dollar sign and does not ask for the weight.
Is this due to my code being wrong or a bug?
#include
int main() {
double height, weight, bmi;
// Ask user for their height
std::cout << "Type in your height (m): ";
std::cin >> height;
// Now ask the user for their weight and calculate BMI
std::cout << "Type in your weight (kg): ";
std::cin >> weight;
bmi=weight/(height*height);
std::cout<<“Your BMI value is " << bmi <<”\n";
return 0;
}
Any help would be appreciated. Thank you in advance.