Problem with Arrays

I've been going on this program for two days now. I was just practising, so I made up a situation for myself and expected to solve it. It just won't work, Ive tried everything I could. My program is supposed to initialize an array, input values for each unit, add the values and display the sum. Instead of doing all that, it's giving me a random number :S. I don't know what I'm doing wrong. Can someone please help me out?

#include<iostream>
using namespace std;

int main()
{

int cost;
int sum;

int computers[5];
for(int i=0;i<5;i++)
{
cout<<"Enter the value for computer #"<<i+1<<": $";
cin>>cost;
computers[i]=cost;
sum+=computers[i];
}

cout<<"The total value of all the computers is $"<<sum<<endl;
}
Last edited on
You have to initialize sum to 0. If you don't, you have no idea what it is and you are just adding to a random number.
Oh wow. I swear I was going insane yesterday over this. Thank you so much.
Topic archived. No new replies allowed.