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?

modify getScores like this:

    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(value >= 0)
        {
          if(count < MAX_SCORES)
          {
            scores[count] = value;
            count++;
          }
        }
      }
      return count;
    }

Okay, I took your advice, but now my code is going into an infinite loop. What do I need to fix, add, or remove to stop this?

you need to enter a negative number :), according to your program spec

I modified the code, but I'm not sure if it is correct because I'm not getting part of the expected output.

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

Here is the output I am getting:

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

Did I misplace the negative number?

Here's a nice way of doing that:

#include <iostream>
using namespace std;
#define SIZE 10
int main(){
    double arr[SIZE], count=0;
    for (int i=0;i<SIZE;cout<<"Nr["<<i<<"]: ", cin>>arr[i++], count++)
        if (arr[i-1]<0) break;
    for (int i=0;i<(int)count;i++)
        cout<<"Nr["<<i<<"]: "<<arr[i]<<"\n";
    return 0;
}

Edited 4 Years Ago by Lucaci Andrew

This article has been dead for over six months. Start a new discussion instead.