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:

Dictator Leader

Let us be clear from the outset, the struggle for indigenous people has always been about land and self-determination, not primarily about r...