Define a getScores() function with the specification and prototype shown below:

// Get scores from console and return the number of scores recorded.
// A negative value signifies end of input.
// If more than MAX_SCORES values are entered then discard entries beyond the first MAX_SCORES.
// Assume that MAX_SCORES is already defined as a global constant

int getScores(double scores[] ) ;

Here is the driver (main()) I used to test my function:

`````` #include <iostream>
#include <iomanip>
using namespace std;
const int MAX_SCORES = 10; // Maximum number of scores

int getScores(double scores[] ) ;
void printScores(double scores[], int numScores);

int main() {
double nums[MAX_SCORES];
int count;
cout << fixed << setprecision(2);

count = getScores(nums);
if (count > 0) {
cout << count << " score"
<< (count == 1 ? " was" : "s were")
<< " recorded: ";
printScores(nums, count);
}
else
cout << "No scores were recorded. " << endl;
return 0;
}

void printScores(double scores[], int numScores) {
for (int i = 0; i <numScores; i++)
cout << scores[i] << " ";
cout << endl;
}
``````

Here is my solution:

``````int getScores(double scores[])
{
int value;
cout << "Please enter up to 10 scores, followed by -1.\n";
for(int count = 0; count <= MAX_SCORES-1; count++)
{
cin >> value;
if(value < 0)
{
scores[count] = value;
}
else
{
return count;
}
}
}
``````

Here are the expected outputs I am trying to get:

Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 6 7 8 9 -1
10 scores were recorded: 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00

Please enter up to 10 scores, followed by -1.
-1
No scores entered.

Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 6 7 8 9 10 -1
Too many scores, the last 1 was discarded
10 scores were recorded: 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00

Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 -1
5 scores were recorded: 0.00 1.00 2.00 3.00 4.00 5.00

My question is: How can I fix the infinite loop get the expected outputs for this program? I've been working on this too many times and I don't know what else to do.

## Recommended Answers

getScores

``````  5. for(int count = 0; count < MAX_SCORES; count++);
``````

## All 2 Replies

getScores

``````  5. for(int count = 0; count < MAX_SCORES; count++);
``````

However, in order to get the desired output; line 15-25 of the main program should be.

`````` if (count > 0){
cout << count << " score" << (count == 1 ? " was" : "s were") << "recorded: ";
printScores(nums, count);

}elseif(count >  MAX_SCORES){
cout<<"Too many scores, the last 1 was discarded "<< endl << count <<"scores were recorded:";
printScores(nums, count);

} else
cout << "No scores were recorded. " << endl;
return 0;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.