What do I need to do in order to fix this infinite loop and get the expected outputs?

Pages: 12
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:

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include 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

Here is my solution:

 ``1234567891011121314151617181920212223242526`` ``````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 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

How do I fix this infinite loop which is still happening in my program? Also, how can I get the expected outputs which has also been a difficult problem for me?
Edit: loop continuous? Or wrong results? Or crashed when printing results?
Last edited on
 `` `` ``if(value != -1 && count <= MAX_SCORES)``

Change '<=' to '<' This redundant operator may cause the program access an invalid variable, then automatically collapse....!!!
Okay, I changed '<=' to '<'. My program is not giving a continuous loop anymore, but it is still printing wrong results.

Here is my output:

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 is the expected output I want 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

Is there something in the if/else if statements I need to add/remove?
Incorrect checking order.
Put "value == -1" {[...]} before "...AX_SCORES" {[...]}

EDIT: No another errors. Your program is fine.
Last edited on
Is this what you meant?

 ``1234567891011121314151617181920212223242526`` ``````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(value == -1) { return count; } else if(count >= MAX_SCORES) { cout << "Too many scores. The last " << count << (count == 1 ? "was" : " were") << " discarded.\n"; return MAX_SCORES; } } }``````

My output is coming out to this:

Please enter up to 10 scores, followed by -1.
54.5 33.5 98.0 79.0 -1
Too many scores. The last 10 were discarded.
10 scores were recorded: 54.00 54.00 54.00 54.00 54.00 54.00 54.00 54.00 54.00 54.00

This is the expected output I need:

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

Are you sure there are no more errors, because I am getting the wrong output?
 ``12`` ``````int value; cin >> value;``````

!?!!!?!!?!?!!?!?!?!?!!!?!?!?!?!?!!?!?!?!!?!!?!!?!!?!!?!!??!?!?!?!?!??!!

You should note 'value' is an int, not float or double.
You may change variables's type to get better result.
Last edited on
I changed that, but right now, it is giving an infinite loop. I don't know what else is there to fix.
 ``123456`` ``````float value = 0; while([...]){ //cin... printf("value : %.3f\n",value); [...]//default }``````

Edit: You may use scanf("%.3f",value) instead of cin
Last edited on
Your second post : only an invaild message appears right?
Then you put "if(value != -1)" before "cout << "Too many"...
Hope it can help.
Last edited on
@andy2012

So you are going back to your original code?

There was only one thing wrong in the other thread. Did you have trouble figuring it out?
This is reference code... (It should correct)
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include const int MAX_SCORES = 10; // Maximum number of scores int getScores(float *scores ) ; void printScores(float *scores, int numScores); int main() { float nums[MAX_SCORES]; int count; count = getScores(nums); if (count > 0) { printf("%d score%s recorded:\n", count , (count == 1) ? " was" : "s were"); printScores(nums, count);} else printf("No scores were recorded. \n\n"); scanf("%d",&count); return 0; } void printScores(float *scores, int numScores) { for (int i = 0; i = MAX_SCORES){ if(value != -1.00f) printf("Enough !!! Too many scores. The last %d was discarded\n\n",count + 1); return MAX_SCORES; } else if(value == -1.00f) { return count; } } } ``````

Edit : Do you understand this code? Do you have any question?

First : (Changed from double to float)
Second :
`value == -1.00f` not `value == -1`
Last edited on
@Jackson Marie (70)

It is clear you don't understand floating point, or the fact that is a C++ forum, C++ code not C code.

See if you can do some research, and tell us why `else if(value == -1.00f)` is always false.

While you are at it, figure out how to format your code with proper indenting.
@TheIdeasMan
Float... !!?!
Have you looked this code carefully? Or just ignore it?
I pretty sure you didn't look too much at this code.
And you should note : THIS IS ONLY A REFERENCE SOURCE
Last edited on
@Jackson Marie (70)

It's called copy & paste.

 THIS IS ONLY A REFERENCE SOURCE
@TheIdeasMan
It's not an advice. It's just a template source (now more accurate). I never request anyone to copy whole my sources to wait....??!?!?
So, seems you, misunderstand???...
Thanks you for a hint!!!

So you don't understand the concept of a forum either.
Say more? It's too brief.
@TheIdeasMan
No need to say more. Stop polluting this thread !!!!
@andy2012
First, ignore TheIdeasMan.
Second, is the rebuilded source correct? I made no noticeable changes, only some tiny code tweaking & optimizations. You should notice and review getSources(), change the double array to float array. "Printf" and "scanf" are popular functions, you want cout simply put out the string & variables from printf or just try your code. (But actually I usually use cout for most cases)... :)
Tell me, and you'll figure it soon.... :)

HTH
Last edited on
OK, I will bite again, just to show how bad Jackson's code is.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include using std::cout; using std::cin; using std::endl; int getScores(double scores[], unsigned MAXSCORES); int main() { const unsigned MAXSCORES = 10; double scores[MAXSCORES]; int count = getScores(scores, MAXSCORES); //print the scores for(int index =0;index < count;index++){ cout << "Score " << index+1 << " is " << scores[index]<< endl; } return 0; } int getScores(double scores[], unsigned MAXSCORES) { double value; //double not int :D int Sentinel = 0; //to check for end of sequence cout << "Please enter up to " << MAXSCORES; cout << " scores, followed by-1.\n"; int count; for(count = 0; count < MAXSCORES; count++) { //do it MAX_SCORES times cout << "Please enter Score Number " << count+1 << "\n"; cin >> value; Sentinel = static_cast(value); //cast to int if(Sentinel > 0) { //not comparing double now scores[count] = value; } else { break; } } return count; }``````

I tested this code on KDevelop - it worked fine for me.

So what do you think andy2012 ? Which code is better in your opinion?

And JM had the nerve to tell me not to pollute the thread.

The reason why I dislike trolls like JM, is they are time wasters - in the same psycho realm as virus writers.

Pages: 12