Code not printing out perfect score

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

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <iostream>
using namespace std;
int countPerfect(int alpha[], int); //function prototype

int main() {
	// your code goes here
	int const size = 20;
	int scores[size];
	
/*
	cout << "Enter a score 0 - 100 (or -1 to quit) :  "; 
	cin >> scores[size];*/

	/*while (scores[size]>-1 && scores[size] <= 100) {*/
	for (int i = 0; i < size; i++) {
		cout << "Enter a score 0 - 100 (or -1 to quit) :  ";
		cin >> scores[size];
		while ((scores[size]<-1) || (scores[size]> 100)) {
			cout << "Invalid number! Please enter a score between 0 - 100 :";
			cin >> scores[size];
		}
		if ((scores[size] == -1) || (size > 20) && (size > 10)) {
			break;
		}
		/*cout << scores[size] << endl;*/
	}
	cout << endl;
	cout << endl;

	

	cout << "There were" << " scores entered with " << countPerfect(scores, size) << " perfect scores." << endl;

	return 0;
}

int countPerfect(int alpha[], int numscore) { // function that counts the amount of perfect scores entered by the user

	int count = 0;
	for (int i = 0; i <= numscore; i++) {
		if (alpha[i] == 100) {
				}
		count++;
	}
	return count;
}
Last edited on
Look at the following snippet:
1
2
3
	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.

@jlb

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.
Last edited on
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.

Why are you using the constant instead of the value that changes every iteration? In your loop what value changes every iteration of the loop?
@jlb

Would I need to change my cin>> to scores[i]? Because that's the value that changes every iteration of the loop.

If so, would I have edit the rest of the for loop to match the new format with "i"?
What do you think?

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.

@jlb

I believe I would have to! I went ahead and made the changes and the program is running perfectly now.

To answer your question it definitely does not make sense. I'll make sure to keep that in mind.

Thank you for the help I appreciate!
Topic archived. No new replies allowed.