Returning a specific value from an array

I have to write a modular program that accepts at least 10 integer test scores from the user and stores them in an array. Then main should display how many perfect scores were entered (i.e., scores of 100), using a value-returning countPerfect function to help it. I also have to validate the input to not accept scores less than 0 or gerater than 100. I have the basics done, but i'm having trouble figuring the rest out. I am new to using arrays.

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
#include <iostream>
using namespace std;

void countPerfect(int intArray[], int perfect); //Function prototype

int main()
{
	int score;
	const int TEST_SCORES = 10;
	int testScores[TEST_SCORES]; // holds test scores for 10 tests
	
	cout << "Enter ten " << TEST_SCORES
		 << "test scores. " ;

	for (score = 0; score < TEST_SCORES; score++)
		cin >> testScores[score]; // I've figured this out

	cout << "The number of perfect scores was";
	countPerfect(testScores, TEST_SCORES);  //need to print actual data, not just spit out whole array

	system("pause");
	return 0;
}

void countPerfect(int nums[], int perfect) //this needs to turn into a function that calculates the number of perfect scores entered
{
	for (int index = 0; index < perfect; index++)
		cout << nums[index]<< "  ";
	cout << endl;
}
Last edited on
So, for your first for loop I would try something like:

for(score = 0; score< TEST_SCORES; score++)
{
cin >> testScores[score];

if(testScores[score] < 0 || testScores[score] >100)
{
cout << "Invalid Score, please enter another: ";
cin >> testScores[score];
}

}

And I could be wrong, but wouldn't you need to use reference arguments with your function?
I was trying to put a while loop inside of the for loop....thanks for the if idea, i don't know why I didn't think of it.

Also, i'm not sure if I would need to use reference arguments...I'm having a tough time with the function.
I'm a little unsure of whether you need reference arguments or not during a function call, but they are definitely needed for returning more than one value from a function call.
Topic archived. No new replies allowed.