Arrray simple program

My Array Program that is really simple will not work. It comes out with just one number. I also keep trying to change the array numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


#include <iostream>
#include <conio.h>


using namespace std;

int main()
{


    int testArray[3] = {1,2,3};
    int sum;


    for (int x = 0; x < 3; x++){
        sum += testArray[x];


    }

    cout << sum << endl;
    getche();
    return 0;
}


The number is comes out with is: 4200831 and then it adds on
Last edited on
You need to initialize sum.

i.e.

int sum = 0;
Try initializing sum with 0, i.e. int sum = 0;
Thanks, it worked
Topic archived. No new replies allowed.