Hey everyone! I'm currently working through a C++ course on Code Academy and I've created a simple calculator that converts weight from Earth to Mars. The problem I'm facing is that the result keeps showing in scientific notation, which I don't want. Plus, the result doesn't seem correct even in that format. I've done some research and tried a few solutions that go beyond the course materials but haven't had any luck. Here's the relevant part of my code:
```cpp
#include
#include
int main()
{
// Mars weight calculator
float weightearth;
float weightmars;
//Calculation for earth weight to mars weight
weightmars = weightearth * (3.73 / 9.81);
//Get weight input from user
std::cout <> weightearth;
//Convert to mars weight
std::cout << fixed << setprecision(2) << "Your weight on mars is " << weightmars << " kg.n";
return 0;
}
```
When I run it, I get:
What is your weight in kg: 74.5
Your weight on mars is 2.643e-310 kg.
That doesn't look right. Any tips on how to fix this?
3 Answers
Also, be sure to initialize your variables to prevent any unexpected behavior! For example, you could start with `float weightearth = 0;` as soon as you declare it. This can be super helpful in catching bugs early on.
Hey! The main issue is that you're calculating `weightmars` before you've actually gotten the input for `weightearth`. You need to move the line `weightmars = weightearth * (3.73 / 9.81);` after you get the input. This way, your calculations will use the correct value.
Here's the adjusted code snippet:
```cpp
std::cout <> weightearth;
weightmars = weightearth * (3.73 / 9.81);
```
Now, `weightmars` will be based on the user's input, and it should display correctly without scientific notation!
Maaaate, you're an absolute gun, thank you!
Had that happen to me too, but can't remember exactly why. I think it was similar to what you faced. But definitely pay attention to where you assign values to prevent having an undefined variable like `weightmars` before you use it.
In case you're still curious, LucidTA got it right. Just make sure inputs are taken first!

On it, thank you!