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[] )
{
cout << "Please enter up to 10 scores, followed by -1.\n";
int value = 1;
int count = 0;
while(value >= 0)
{
cin >> value;
if(count <= MAX_SCORES && value >= 0)
{
scores[count] = value;
count++;
}
if(count > MAX_SCORES)
{
return(MAX_SCORES);
}
else
{
return(count);
}
return(scores[MAX_SCORES]);
}
}
Here is the output I am only getting:
Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 6 7 8 9 -1
1 score was recorded: 0.00
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 get the expected outputs for this program? Am I missing some code that needs to be added?