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.

2 Likes

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

1 Like

There are some logical error with your code, you can try this code to fix the error.

#include <iostream>

int main() {
    // Declare variables
    double WeightOnMars;
    double WeightOnEarth;

    // Prompt the user to enter their weight on Earth
    std::cout << "Enter your weight on Earth (in KG): ";
    std::cin >> WeightOnEarth;

    // Calculate the weight on Mars using the conversion factor (3.73 / 9.81)
    WeightOnMars = WeightOnEarth * (3.73 / 9.81);

    // Display the weight on Mars
    std::cout << "Your weight on Mars would be approximately " << WeightOnMars << " KG\n";

    return 0;
}

Thanks