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:

If a hater attacked your age and not the goodness of you

Whether young or old, I've always been known what endures. I've known the very idea of people that were all created equal and deserv...