I don't understand why it gives me wrong answers?

I'm going through the basics right now and reading books to seriously get into c++.
But I don't understand whats wrong with my code ?
For example if i write numbers 5 for a and 5 for b it gives me answer 21 when it should be 10?

Here's the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a;
    int b;
    int z;
    z = a + b;
    cout<<"Put numbers for a and b"<<endl;
    cin>>a;
    cin>>b;
    cout<<"The sum of the numbers is  "<< z <<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
You add a and b before you read them. Their default values could be anything.
Solution: move z = a + b; after the the second cin.
Last edited on
thank you smart sir.
Topic archived. No new replies allowed.