FAQ: Variables - Review

#include

int main() {
// Add your code below
//Weight_Mars (in kg) = Weight_Earth (in kg) x (g_Mars / g_Earth)

double weight_earth = 0;

double weight_mars;

const double g_earth = 9.8, g_mars = 0.38 * g_earth;

std::cout << "Enter the weight on Earth :" << weight_earth << "\n" ;

std::cin >> weight_earth;

weight_mars = weight_earth * (g_mars / g_earth);

std::cout << "you Weigh on Mars is: " <<  weight_mars  << "\n";
 
 return 0;

}

Here is code that may help you.
#include

int main() {
// Add your code below
//Weight_Mars (in kg) = Weight_Earth (in kg) x (g_Mars / g_Earth)

double weight_earth = 0;

double weight_mars;

const double g_earth = 9.8, g_mars = 0.38 * g_earth;

std::cout << "Enter the weight on Earth :" << weight_earth << "\n" ;

std::cin >> weight_earth;

     weight_mars = weight_earth * (g_mars / g_earth);

std::cout << "you Weigh on Mars is: " <<  weight_mars  << "\n";
 
 return 0;

}

Hi there

so I didn’t enter any value for miles and It kind of went through, help!

#include

int main() {

// Add your code below

double weightEarth;

double weightMars;

std::cout << "Enter how much weight It on earth : ";

std::cin >> weightEarth;

weightMars = (weightEarth/9.81)*3.711;

std::cout <<“Your weight in Mars is “<<weightMars<<”\n”;

double miles;

double kilometers;

std::cout <<"Now enter a distance in miles: ";

std::cin >> miles;

kilometers = miles*1.609344;

std::cout << kilometers<<" Kilometers\n";

}

You have 64,5
… try that without the ,
I think it’ll only accept decimals using . anyway: 64.5

1 Like

Thanks! It worked! :smiley: :smiley: :smiley: :smiley: :smiley:

can anyone help me with this code?
weight is 45 kg and if i at mar, my weight will be “weight * 37.83 / 100 = 17.0235” but my code ouput is -3.87569e+08 kg

#include <iostream>

int main() {
  int weight;
  float weightmar = weight * 37.83 / 100;
  std::cout << "ur weight is: \n";
  std::cin >> weight;  
  std::cout << "ur weight at mar is: " << weightmar << " kg" << std::endl;
}

Your statements will be executed top to bottom.

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

An uninitialized variable will result in undefined behavior (in your case it happens to be garbage/nonsense output).

See: Uninitialized variables and undefined behavior

Then, you prompt the user for input and assign it to weight. But, your calculation for weightmar 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 weight first and then perform the calculation for weightmar.