If you use std::vector ( link ), you can add elements and change the dynamic vector. This size can be easily requested using the size() method. If you use such arrays, you must track the number of elements in it yourself.
If you have a vector filler with elements, your loop might look like this:
std::vector<int> scores;
If you need to use arrays and actually have a scoreCount variable with the number of real values entered, just use this in your loop:
for (int i=0; i<scoreCount; i++) {
The third option, as I mentioned in the comments, will initialize the entire array with a value that you never use (usually -1), and then use this as a marker for filled or empty positions as an array:
for (int i=0; i<1000; i++) { scores[i] = -1; } // add real values to scores int i=0; while (scores[i] != -1 && i < 1000) { // use value i++; }
Victor sand
source share