a + b = A Really Wrong Number!! Why?

Hi there, I just started learning C++ as my first language and I don't understand why I'm having this problem. I know it's most likely a very very newbie question but I'd like to understand it before I move onto something new.

This code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum = a + b;

    cout << "Enter a number please: \n";
    cin >> a;

    cout << "Enter a SECOND number please \n";
    cin >> b;

    cout << "The calculation is: " << sum << endl;

    return 0;

}


Gives me the answer -1534493305 if I add 2 + 2 together. Where as if instead I do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum;

    cout << "Enter a number please: \n";
    cin >> a;

    cout << "Enter a SECOND number please \n";
    cin >> b;

    sum = a + b;
    cout << "The calculation is: " << sum << endl;

    return 0;

}


It gives me the correct answer of 4. I'm not sure why. What's the computer doing that I don't know?

Thanks,
Scott

What's the computer doing that I don't know?

it's doing exactly what you've told it to do.

In the first piece of code you calculate sum BEFORE you've asked the user for values for a and b, so you're going to get a whacky result for sum.

In the second piece of code you (correctly) wait until you've asked the user for a and b and then calculate sum.

edit:
it is also very good practice to initialise all variables. For example:

1
2
3
    int a = 0;
    int b = 0;
    int sum = 0;
Last edited on
Ahh I see. So the computer just works its way down the program in order. That makes sense. Thank you!


it is also very good practice to initialise all variables.

Why? I'm not sure what that does. Wouldn't the outcome be the same?
In this case maybe, although if you had initialised them you'd have got zero printed to the screen rather than -1534493305.

I'm not sure what that does.


It is also good to initialise as it helps with debugging as well (i.e. when you are stepping through your code line by line to find a bug).

Do you honestly think variables initialised to random values is better? Have a read of this:
http://programmers.stackexchange.com/questions/223862/how-important-is-to-initialize-a-variable


Last edited on
it is also very good practice to initialise all variables.

Why? I'm not sure what that does. Wouldn't the outcome be the same?


It's good practice as one of the steps to reduce the possibility of bugs in the code.

Let's say that in the first case (where the code was incorrect), the values of a and b just happened to be -75385889 and 75385893.
Then the value of a + b is calculated giving the result 4.


Next the user enters the input 2 and 2, the computer prints the result 4 and hey presto, it looks like the program is working perfectly. At least if you set a, b and sum to zero at the start and see the result zero printed you have some idea that your input is being ignored.

Last edited on
If you don't initialize local variables (those defined inside a function) then their initial value will be undefined - most likely whatever value happens to be in the memory there the variables are located, but technically using an uninitialized behavior is "undefined behavior" and the program can do whatever it wants after that.

So since you're new to C++, I recommend that you initialize every variable. Bugs from uninitialized behavior can be very strange, as you've discovered.
closed account (48T7M4Gy)
By initializing a and b you control the junk you had in the first prog. So int a = 0, b = 0; puts a bit of control on the program. Not over important here because a and b get overwritten by the 'cin' but can be quite significant where the starting point for doing a calculation might need to be tightly controlled.

http://www.cplusplus.com/doc/tutorial/variables/
Thank you everyone for your generous and informative information! I understand why I was having the problem and learned that I should initialise my variables. Once again, thank you all for your wonderful replies - expect to see me here again in the future!
Topic archived. No new replies allowed.