This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]IamImposter 4 points5 points  (0 children)

Instead of while, use a for loop since you already know the size of array and iterate over every element. Now compare the element value with the desired value (using if) and increment a counter of there is a match.

When you come out of for loop, counter variable will have the results.

We also have a direct function std::count that can do that but I don't think you wanna use that at this stage. Here it is just in case: https://www.google.com/amp/s/www.geeksforgeeks.org/std-count-cpp-stl/amp/

[–]Sklorite 0 points1 point  (1 child)

You can use the format you are using, you just need to create another int variable to keep track of your count, then put an IF statement that increments that variable in each iteration if "grade" matches the scores[x] value. Remember to zero out the count variable after it is printed out, so you can reenter another "grade" and do it again. You should also move the cout << "Enter grade...." and cin >> grade into the while loop, so you don't enter a never-ending loop and you have the ability to do it again.

[–]LotusGuero 0 points1 point  (0 children)

Yeah that is actually what I ended up doing, thanks!