Getting a garbage value.

Hello, first time poster - Have been meaning to register for a long time.. But i haven't gotten around to it until now. So expect alot of noob question, hehe.

So i'm writing a simple program that calculates how many earth years x jupiter years would be.

1 jupiter year = 12 earth years.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
    int eyears, jyears;
    eyears = jyears * 12;
    cout << "Input the number of Jupiter years to convert to earth years: ";
    cin >> jyears;
    cout << jyears << " years on jupiter is equal to: " << eyears;
    cout << "\n";
    system("pause");
    return 0;
}


The application runs fine, but i'm getting a garbage value instead of whatever jyears*12 is.

I'm missing something really simple here... Any ideas?

Edit: Err, lol. Sorry, that is a very very bad misstake... I'm used to using loops so that variable changes are always updated. Thus i didn't react to eyears = jyears * 12 actually being translated to 0 * 12.

I've got another question though, why would the compiler make a garbage value out of 0 * 12 instead of actually displaying 0?
Is it because i'm using a variable as the "0" in it?
Last edited on
You haven't initialized jyears. You defined eyears and jyears without intializing a value. reading an uninitialized value such as your jyears produces undefined behavior. Undefined behavior on some systems (embedded comes to mind) can crash the system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
    int eyears = 12, jyears; // You don't have to initialize jyears now since you initialize it with the cin >> jyears statement

    cout << "Input the number of Jupiter years to convert to earth years: ";
    cin >> jyears;
    cout << jyears << " years on jupiter is equal to: " << jyears * eyears;
    cout << "\n";
    system("pause");
    return 0;
}
I've got another question though, why would the compiler make a garbage value out of 0 * 12 instead of actually displaying 0?
Is it because i'm using a variable as the "0" in it?

It's a good queston. It's because when vairables(ints for example) are created they are not initialized to 0 but some random number. Hence why you are getting a garbage value; you're actually getting 12 * the random number that jyears has when it is created.
Aha! I always thought variables without any specified initialization value would be initialized to 0.

Thanks :)
What happens if you press the wrong key accidentally and jyears is left uninitialized? You'll be right back where you started printing garbage back to the console. At the very least initialize jyears to 0 so that a known value is printed if cin fails. Better yet, create a while loop which loops until a valid piece of data is entered.

while(!(cin >> jyears));
I always thought variables without any specified initialization value would be initialized to 0.

In case the variable is static, it's initialized to 0, e.g. static int i;. More accurately, a static variable is initialized to binary zeros. But don't use static storage just because of the automatic initialization; initialize your variables manually to 0 (or whatever) unless you specifically need a static variable.
Kempofighter, that's a great thing to remember... I never consider any failures in my programs, since i'm still learning and i'm the only one using them - thanks for letting me know! :)
Topic archived. No new replies allowed.