What is wrong with the math?

what is going on with this math? i searched up how to convert the weight of something on mars for this assignment, and the formula isn’t working for some reason. what am i doing wrong?

Here is the code:

#include

int main() {

// Add your code below

int WeightOnMars;

int WeightOnEarth;

WeightOnMars = WeightOnEarth * (3.73 / 9.81);

std::cout << “Enter your weight\n”;

std::cin >> WeightOnEarth;

std::cout << " Your weight is " << WeightOnMars << " KG\n";

}

Your statements will be executed top to bottom.

You are declaring the WeightOnEarth variable and then using it to calculate WeightOnMars. But, at this point, WeightOnEarth is uninitialized.

An uninitialized variable will result in undefined behavior.

See: Uninitialized variables and undefined behavior

Then, you prompt the user for input and assign it to WeightOnEarth. But, your calculation for WeightOnMars has already been performed, so this input value doesn’t make a difference.

Instead you should consider re-arranging your code so that you get and assign the user input to WeightOnEarth first and then perform the calculation for WeightOnMars.

1 Like

if you want decimal answers, you may need to change int to float or double

1 Like