mercredi 29 juin 2016

Why does age*12 produce a garbage value? [duplicate]

This question already has an answer here:

I'm just wondering why my code produces a garbage value.

#include "std_lib_facilities.h" // cool lib from bjarne.

int main()
{
    cout << "Please enter your first name and age (followed by pressing 'enter'\n";

    string first_name = "???";
    int age;
    int age_12 = age * 12;

    cin >> first_name >> age;
    cout << "Hello, " << first_name << " (months " << age_12 << ")!\n";

    return 0;
}

I know that you can do

cout << "Hello, " << first_name << " (months " << age * 12 << ")!\n";

Which solves the problem but I'm just interested in why it produces a garbage value.

Aucun commentaire:

Enregistrer un commentaire