Q:

I am trying to do basic addition in c++ but large numbers are showing

I am tryimg to do basic addition in c++ but a large number shows instead of the number thats suposed to show.
#include <iostream>

using namespace std;

int main()
{
    int x;
    int y;
    int sub = x + y;

    cout<<"Enter First number:"<<endl;
    cin>>x;
    cout<<"Enter second number:"<<endl;
    cin>>y;
    cout << "The sum is: "<< sub << endl;
    return 0;
}

When I run this it shows the sum as "6996596".

A:

sub is being created and attributed before x and y are set. When you declare the variables but don't initialize then, you get unknown values from what you had previously on the stack.
If you move the sub variable attribution to after x and y were read from the terminal, the program would work correctly.

A:

You are computing your sum before accepting user input. Furthermore, you are summing two uninitialized variables, leading to undefined behavior. 

