I had to write a modular program that accepts at least 10 integer scores but no more than 20 and displays how many perfect scores where entered. I cannot figure out why it keeps displaying 21 and the amount of perfect scores. Here is the code
for (int i = 0; i < size; i++) {
cout << "Enter a score 0 - 100 (or -1 to quit) : ";
cin >> scores[size];
Do you realize that "size" is a constant that is the size of your array? Do you realize that using this value for the index of your array produces undefined behavior because it is accessing your array out of bounds.
Next look at this snippet: for (int i = 0; i <= numscore; i++) {
Do you realize that you will try to access your array out of bounds on the last iteration of this loop?
Remember arrays start at zero and end at size - 1. So using the operator<= is usually a mistake.
I do realize that "size" is a constant and that it is the size of my array. I was not aware of that to be honest. I thought what I was doing with that for loop right there was storing the inputted values into array.
I'm still a little unsure on what I would need to change it too other than 19 so it doesn't go outside of my bounds.
However I do see your point in the last snippet. I've managed to fix that issue with the for loop.
Does it make sense to try to access an array out of bounds?
That's what happens if you try to use size to access the array, and remember accessing the array out of bounds invokes undefined behavior which is never a good thing.