How can I get the expected outputs for this C++ program?

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:

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
31
32
#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:

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
int getScores(double scores[] )
 {
 int value;
 int count = 0;
 cout << "Please enter up to 10 scores, followed by -1.\n";
 while(value != -1)
 {
 cin >> value;
 if(value != -1 && count <= MAX_SCORES)
 {
 scores[count] = value;
 count++;
 }
 else if(count >= MAX_SCORES)
 {
 cout << "Too many scores. The last " << count
 << (count == 1 ? " was" : " were") 
<< " discarded\n ";
 return MAX_SCORES;
 }
 else if(value == -1)
 {
 return count;
 }
 }
 }


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
Too many scores. The last 10 were 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


Here are the expected outputs I need to get:

Please enter up to 10 scores, followed by -1.
54.5 33.5 98.0 79.0 -1
4 scores were recorded: 54.50 33.50 98.00 79.00

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

What I can do to fix this problem? Do I need to change some lines of code?
You are testing for too many scores after the count is incremented, so it is is counting the -1 entry as well.

Have you thought about using a for loop to get the input - then the user only be able to enter the specified amount of scores.

Hop all goes well.
Here is my updated solution, but I don't know if this is what you meant:

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
int getScores(double scores[] )
{
	int value;
	int count;
	cout << "Please enter up to 10 scores, followed by -1.\n";
	for(value = 0; value < MAX_SCORES; value++)
	{
		cin >> value;
		if(value != -1 && count <= MAX_SCORES)
		{
			scores[count] = value;
		}
		else if(count >= MAX_SCORES)
		{
			cout << "Too many scores. The last " << count
			<< (count == 1 ? " was" : " were") 
			<< " discarded\n ";
			return MAX_SCORES;
		}
		else if(value == -1)
		{
			return count;
		}
	}
}
this for(value = 0; value < MAX_SCORES; value++)
might be
for(count = 0; count < MAX_SCORES; count++)
Last edited on
I tried using that line of code, but when I did, my program was giving an infinite loop. I still don't know what to do.
1
2
3
4
5
6
7
else if(count >= MAX_SCORES)
		{
			cout << "Too many scores. The last " << count
			<< (count == 1 ? " was" : " were") 
			<< " discarded\n ";
			return MAX_SCORES;
		}


You don't need this bit, because the for loop will only loop MAX_SCORES number of times.

And the if only needs to be this:

1
2
3
4
5
6
if(value != -1 ) {  //braces not needed but I put them there in case more code is added later
     scores[count] = value;
}
else {
   return count;
}


HTH
Okay, I took your advice and fixed my code again, but it's still giving the incorrect output.

Here is my source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int getScores(double scores[] )
{
	int value;
	int count;
	cout << "Please enter up to 10 scores, followed by -1.\n";
	for(count = 0; count <= MAX_SCORES-1; count++)
	{
		cin >> value;
		if(value != 1)
		{
			scores[count] = value;
			value++;
			return value;
		}
		else
		{
			return count;
		}
	}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getScores(double scores[] ) {
	int value;  //the value entered by user
	int count;  //this is only used in the loop, initialise in for loop, no need to worry about scope 
	cout << "Please enter up to 10 scores, followed by -1.\n";
	for(int count = 0; count <= MAX_SCORES-1; count++) {
		cin >> value;
		if(value != 1) {
			scores[count] = value;
			value++;  //this is the number entered - no point incrementing it
			return value;  //count is what you want, not the last value entered
		}
		else {
			return count;  //this is right
		}
	}
}


HTH
I fix my code again and again, and it is not giving the correct output. I'm sorry, but I don't know what else to fix and/or add.

Here is my solution which is still not correct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int getScores(double scores[] )
{
	int value;
	cout << "Please enter up to 10 scores, followed by -1.\n";
	for(int count = 0; count < MAX_SCORES; count++)
	{
		cin >> value;
		if(value != 1)
		{
			scores[count] = value;
			return scores[count];
		}
		else 
		{
			return count;
		}
	}
}
I gave it to you exactly in my last post - why did you change it? The strike through means delete that part

Your current version only allows the input of one number then it returns.

Crikey, I forgot that the parameter needs to be a reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
int getScores(double scores[]& ) {
	int value;  //the value entered by user
	cout << "Please enter up to 10 scores, followed by -1.\n";
	for(int count = 0; count <= MAX_SCORES-1; count++) {
		cin >> value;
		if(value != 1) {
			scores[count] = value;
		}
		else {
			return count;  
		}
	}
}
I added that reference variable, but now it is giving an error message:
error: expected ',' or '...' before '&' token

I'm not sure what that means, but does the reference parameter need to be placed somewhere else in the function call?

By the way, sorry I changed the previous code.
Sorry brain fart on my part (:+D)

Leave it as it was - without the &.
I removed the reference variable, but my code is still giving an infinite loop. What more do I need to fix or add because this is stressing me out?

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 != 1)
		{
			scores[count] = value;
		}
		else 
		{
			return count;
		}
	}
}
Awwww crap !!

1
2
3
if(value < 0) //negative value to quit !!!
                                //I don't like doing this with doubles
                                //but it should work 


One day I will learn to read properly !!

Mind you, it is a bit of a lesson for you too - need to think carefully about what is going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int getScores(double scores[])
{
	double value; //double not int :D
        int Sentinel = 0; //to check for end of sequence

	cout << "Please enter up to 10 scores, followed by -1.\n";
	for(int count = 0; count < MAX_SCORES; count++) { //do it MAX_SCORES times didn't see that before
		cin >> value;
                Sentinel = static_cast<int>value; //cast to int

		if(Sentinel < 0) {  //not comparing double now
			scores[count] = value;
		}
		else {
			return count;
		}
	}
}


We will get there yet !!
The code is still giving an infinite loop. What else should I add/fix to get rid of this infinite loop and get the expected outputs?
All right I don't know why I am so discombobulated today. I can see that I have made another dumb & simple mistake. I would be more productive at the pub!!

It will be good for you to figure this out yourself - what is going on here? Think about the logic.
Checking compiler warnings might help you figure it out.
Topic archived. No new replies allowed.