Tuesday

Variable is not initialized problem

Code

#include <iostream>

using namespace std;

 

int main()

{

    int sum;

 

    int n1;

    int n2;

 

    cout << "Input n1 => ";

    cin >> n1; // 1

    cout << "Input n2 => ";

    cin >> n2; // 2

 

    sum = sum + n1;

    sum = sum + n2;

 

    cout << "sum: " << sum << endl; // 32769

 

    return 0;

}


Sample output 

Input n1 => 1

Input n2 => 2

sum: 32769


Question: Why and how to get the correct output which is “sum: 3”?

No comments:

Fire survival tips

1 Stop, drop, cover and roll  If your clothes catch fire stop, drop, cover your face with your hands and roll to smother the flames. 2 Get d...