Tuesday

Logic error and output error: array size problem

Question: Why and how to get the correct output 
which is "Sum: 5" after you have entered 1 five times?

#include <iostream>
using namespace std;

int main()
{
int mysize = 5;
int myarray[4];

cout << "Enter five numbers and calculate sum" << endl;
for (int i=0; i<mysize; ++i)
{
cout << "Enter number " << i+1 << " => ";
cin >> myarray[i]; // to enter 1 five times
}

int sum = 0;
for (int i=0; i<mysize; ++i)
{
sum += myarray[i];
}
cout << "Sum: " << sum << endl; // sum 1 is wrong

return 0;
}

No comments:

Fire survival tips

1 Stop, drop, cover and roll  If your clothes catch fire stop, drop, cover your face with your hands and roll to smother the flames. 2 Get d...