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.

#include <iostream> int main() { // Add your code below double ilb = 0; double mars = (ilb / 100) * 38; std::cout << "What is your weight?\n"; std::cin >> ilb; std::cout << "You would weigh " << mars << " on mars!\n"; std::cout << "Your looking thinner already!\n"; }

Above is some code i made for the mars idea. I thought putting both concempts of cin and math would be alright. I pass the reveiw just fine yet i cant get this to output anything other than zero.

what im looking for it todo is ask for a weight and then turn that into 38% of what they put. At first i put the equation as mars = ilb * .38, then revised it to ilb * 0.38. Did the final revision to see if it could make it alittle simpler. And yet it still keeps outputting 0. Any ideas?

Your statements will be executed top to bottom. You have initialized ilb as 0 and then calculated mars based on this initial value. Unsurprisingly, mars is 0 (because 38% of 0 is 0). You later prompt for a value of ilb. But just because the value of ilb changes doesn’t mean that the calculations in the previous statements will be automatically updated. mars is still 0. You should consider moving your mars calculation statement below the cin.

Ok, i think i got ya. What i thought was that zero would be changed and that it was a placeholder. So if change it to

#include

int main() {
// Add your code below
double ilb = 0;

std::cout << “What is your weight?\n”;
std::cin >> ilb;

double mars = (ilb / 100) * 38;

std::cout << “You would weigh " << mars << " on mars!\n”;
std::cout << “Your looking thinner already!\n”;

}

It should fix it?

Yes   

Awesome, i edited the code and it works great! Thanks a bunch!

1 Like